Easy Solution in C++, no Dynamic Allocation


  • 0
    F
        bool canPermutePalindrome(string s) {
            int alpha[256] = { 0 };
            int odds = 0;
            for (char c : s) alpha[c]++;
        
            for (int i = 0; i < 256; i++)
                if ((alpha[i] % 2) != 0) ++odds;
        
            return odds <= 1;
        }
    

  • 0
    E

    @fek I think you can return early by checking odds inside “for” loop like this.

            if ((alpha[i] % 2) != 0) 
    	  {
    	    ++odds;
    	    if(odds > 1)
    		return false;
            }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.