Count the number of
palindromes in a string
January 28, 2016
Write down ideas:
1. First of all, do not count duplicate.
2. Brute force solution:
any substring of O(N^2) substrings to see if it is a palindrome;
Add the substring of palindrome to a hashset if it is not in the hashset.
And return the length of hashset
3. Use recursive solution - using subproblem to solve. Cannot filter out duplicate - not good
4. Better solution - use center point of string - 2n + 1, and then, go over each one, add all palindromes substring.
Requirement: write a C# code in 10 minutes for the solution, using brute force one.
https://github.com/jianminchen/AlgorithmsPractice/blob/master/NumberOfDistinctPalindromes.cs
No comments:
Post a Comment