jayati

Longest Palindrome

Mar 17th, 2023
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int longestPalindrome(string s) {
  4.         int oddCount = 0;
  5.         unordered_map<char, int> ump;
  6.         for(char ch : s) {
  7.             ump[ch]++;
  8.             if (ump[ch] % 2 == 1)
  9.                 oddCount++;
  10.             else    
  11.                 oddCount--;
  12.         }
  13.         if (oddCount > 1)
  14.             return s.length() - oddCount + 1;
  15.         return s.length();
  16.     }
  17. };
  18.  
Advertisement
Add Comment
Please, Sign In to add comment