-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPalindromePermutation.cs
29 lines (25 loc) · 1012 Bytes
/
PalindromePermutation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using PracticeQuestionsSharp.Helper;
namespace PracticeQuestionsSharp.Exercises.Strings
{
public static class PalindromePermutation
{
//Determine whether a string is a permutation of a palindrome.
public static bool IsPalindromePermutation(string s)
{
int singleCharCount = 0;
s = s.SortString().ToLower();
for (int i = 0; i < s.Length; ++i)
{
if (i + 1 >= s.Length) break;
if (s[i] == s[i + 1]) i++; //we can ignore the next character because it has a pair
else
{
singleCharCount++;
if (singleCharCount > 0 && s.Length % 2 == 0) return false; //cannot have unpaired character in an even length palindrome
if (singleCharCount > 1 && s.Length % 2 == 1) return false; //odd length palindromes have a single unpaired character
}
}
return true;
}
}
}