Check permutations of a string for palindromes

Problem: Given a string, check if any permutations of that string are a palindrome

Solution 1: Use a hashtable to count each time a character is used

public bool IsPalindrome(string str)
{
  var hash = new Dictionary<char, int>();
  foreach (char c in str)
  {
    if (hash.ContainsKey(c))
    {
        hash[c]++;
    }
    else
    {
      hash.Add(c, 1);
    }
  }

  if (hash.Values.Count(v => v % 2 != 0) > 1)
  {
    return false;
  }

  return true;
}

Leave a comment

Design a site like this with WordPress.com
Get started