palindrome
Question 1
Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.What's very smart here is start from a character, and get its i-1 and i+1 for compare.
1 public String longestPalindrome(String s) { 2 if (s == null || s.length() <=1) 3 return s; 4 5 int start = 0; 6 int longest = 0; 7 for (int i = 0; i < s.length();){ 8 int p1 = i; 9 int p2 = i; 10 while(p2<s.length()-1 && s.charAt(p2) == s.charAt(p2+1)){ 11 p2 ++; 12 } 13 i = p2 + 1; 14 while(p1>0 && p2<s.length()-1 && s.charAt(p1-1) == s.charAt(p2+1)){ 15 p1--; 16 p2++; 17 } 18 int len = p2 - p1 + 1; 19 if (len > longest){ 20 start = p1; 21 longest = len; 22 } 23 } 24 return s.substring(start, start + longest); 25 }
Question 2
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.1 public boolean isPalindrome(int x) { 2 if (x < 0){ 3 return false; 4 } 5 int base = 1; 6 while (x/base >= 10){ 7 base *= 10; 8 } 9 while (x != 0){ 10 int left = x / base; 11 int right = x % 10; 12 if (left != right){ 13 return false; 14 } 15 x = x % base; // filter the most important digit 16 x = x / 10; // filter the least important digit 17 base = base / 100; // divide by 100, because already filter 2 digits 18 } 19 return true; 20 }
Question 3
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
1 public boolean isPalindrome(String s) { 2 s = s.trim(); 3 s = s.toLowerCase(); 4 int left = s.length() - 1; 5 int right = 0; 6 7 while (left >= right){ 8 char c1, c2; 9 if (!Character.isLetterOrDigit(s.charAt(left))){ 10 left --; 11 } 12 else if (!Character.isLetterOrDigit(s.charAt(right))){ 13 right ++; 14 } 15 else{ 16 c1 = s.charAt(left); 17 c2 = s.charAt(right); 18 if (c1 != c2){ 19 return false; 20 } 21 left --; 22 right ++; 23 } 24 } 25 return true; 26 }
Question 4
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
1 public List<List<String>> partition(String s) { 2 List<List<String>> res = new ArrayList<List<String>>(); 3 dfs(new ArrayList<String>(), s, res); 4 return res; 5 } 6 //depth-first-search 7 public void dfs(List<String> path, String s, List<List<String>> res){ 8 if(s.length() == 0){ 9 res.add(path); 10 return; 11 } 12 for(int i = 1; i <= s.length(); i++){//i start from 1, to use the substring method 13 if(isPalin(s.substring(0,i))){ 14 List<String> extendedPath = new ArrayList<String>(path); 15 extendedPath.add(s.substring(0,i)); 16 dfs(extendedPath, s.substring(i),res); 17 } 18 } 19 } 20 public boolean isPalin(String s){ 21 for(int i = 0; i < s.length()/2; i++){ 22 if(s.charAt(i) != s.charAt(s.length()-1-i)) return false; 23 } 24 return true; 25 }
Question 5
Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
1 public int minCut(String s) { 2 int min = 0; 3 int len = s.length(); 4 boolean[][] matrix = new boolean[len][len]; 5 int cuts[] = new int[len+1]; 6 7 if (s == null || s.length() == 0) 8 return min; 9 10 for (int i=0; i<len; ++i){ 11 cuts[i] = len - i; //cut nums from i to len [i,len] 12 } 13 14 for (int i=len-1; i>=0; --i){ 15 for (int j=i; j<len; ++j){ 16 if ((s.charAt(i) == s.charAt(j) && (j-i<2)) 17 || (s.charAt(i) == s.charAt(j) && matrix[i+1][j-1])) 18 { 19 matrix[i][j] = true; 20 cuts[i] = Math.min(cuts[i], cuts[j+1]+1); 21 } 22 } 23 } 24 min = cuts[0]-1; 25 return min; 26 }