C# using array as hash O(n) time.


  • 0
    Y
    public class Solution {
        public int LongestPalindrome(string s) {
            int[] charList=new int[128];
            int count=0,singleChar=0;
            for(int i=0;i<s.Length;i++)
            {
                if(charList[s[i]-'A']==1)
               {
                   charList[s[i]-'A']--;
                   count += 2;
                   singleChar--;
               }
               else
               {
                   charList[s[i]-'A']++;
                   singleChar++;
               }
            }
            return count+(singleChar>0?1:0);
        }
    }

Log in to reply
 

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