O(1) space using sorting c++


  • 0
    S
    class Solution {
    public:
        bool canPermutePalindrome(string s) {
            std::sort(s.begin(), s.end());    
            
            int l=1;
            int odd=0;
            
            for (int i=1;i<s.size();++i) {
                if (s[i] == s[i-1]) ++l;
                else {
                    if (l % 2 != 0) ++odd;
                    l=1;
                }
            }
            
            // for the exit edge 
            //
            if (l % 2 != 0) ++odd;
            
            if (s.size()%2==1) {
                return (odd == 1);
            } else {
                return (odd == 0);
            }
        }
    };
    

Log in to reply
 

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