Monday, February 6, 2012

String Palindromes

1. String is a palindrome or not
http://www.programcreek.com/2013/01/leetcode-valid-palindrome-java/
http://quiz.geeksforgeeks.org/c-program-check-given-string-palindrome/
http://algorithms.tutorialhorizon.com/find-whether-given-string-is-palindrome-or-not/

2.Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
http://www.programcreek.com/2014/05/leetcode-palindrome-pairs-java/

3.Longest Palindrome in a string
How will you find the longest palindrome in a string? I.e. if the string is XMADAMYX, Your code should print MADAM.

http://www.leetcode.com/2011/11/longest-palindromic-substring-part-i.html
http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/
http://www.geeksforgeeks.org/longest-palindromic-substring-set-2/

http://stevekrenzel.com/articles/longest-palnidrome


4.Palindrome Partitioning:
http://www.programcreek.com/2013/03/leetcode-palindrome-partitioning-java/
http://www.programcreek.com/2014/04/leetcode-palindrome-partitioning-ii-java/

5.Next higher Palindrome
Given a number, you have to compute nearest palindrome number. This palindrome number should be greater than given number.

Concept:
If number of digits are even, flip the most significant first half towards lower significant half that makes the palindrome. If the palindrome is smaller than the original, add one to the most significant half and then flip it over. Example, number -2345, most significant half -23, first candidate palindrome – 2332. Since, this is less than 2345, we add 1 to most significant half which makes it 24. Our next candidate for palindrome becomes 2445.
If number of digits are odd, we should not flip the middle one. For example, 45436, most significant digits will be 454 and our candidate for palindrome will be 45454. Since, it is greater than original, we’ll keep this as answer.
In case, adding one adds another digit to the number, we have to switch the process as per new number of digits.

6.Integer's Bit pattern is palindrome or not
How can you determine if a positive integer is a palindrome. For example, 5 is a palindrome because 5 = 101. Also, 9 is a palindrome because 9 = 1001.

Strategy:
An integer is palindrome when its bit representation is the same reading from left to right or from right to left. Thus, in order to know if it is a palindrome or not, we just have to check if the number's value is still the same when we read the number's bits backward (right to left). For example, 5 is 101 and when we read its bits from right to left, which is 101, the result is still 5. However, 4 is 100 and when read backward it becomes 001 which is 1, so 4 is not a palindrome.

Algorithm:
integer nResultNum = 0
integer nInputNumCopy = nInputNum

while nInputNumCopy > 0
  nResultNum = nResult << 1;
  if nInputNumCopy & 1 == 1
    nResultNum = nResultNum | 1;
  nResultNumCopy = nResultNumCopy >> 1;
end while

if nResultNumCopy == nInputNum
  return true

return false

1 comment:

  1. http://www.geeksforgeeks.org/archives/11871
    http://www.geeksforgeeks.org/archives/11902
    http://www.geeksforgeeks.org/archives/11937

    ReplyDelete