leetcode 409. Longest Palindrome

Orignal question can be found here.

My solution is as follows:

int longestPalindrome(char* s)
{
  int result = 0;
  int one_more = 0;
  int hash[52]={0};
  for (int i=0; i<strlen(s); ++i)
    {
      if(s[i]>='a' && s[i]<='z')
        ++hash[s[i]-'a'];
      else if(s[i]>='A' && s[i]<='Z')
        ++hash[s[i]-'A'+26];
    }
  for (int i=0; i<52; ++i)
    {
      if(hash[i]%2 == 1)
        one_more = 1;
      result += (hash[i]/2)*2;
    }
  if(one_more == 1)
    ++result;
  return result;
}

Leave a Reply

Your email address will not be published. Required fields are marked *