866 Prime Palindrome
Find the smallest prime palindrome greater than or equal to N.
Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1.
For example, 2,3,5,7,11 and 13 are primes.
Recall that a number is a palindrome if it reads the same from left to right as it does from right to left.
For example, 12321 is a palindrome.
- 對偶數位的palindrome,一定能夠被11整除,除了11本身以外,都不是prime。所以只考慮奇數位的palindrome。滿足條件的兩位數且是prime的palindrome只有11,接下去考慮三位數,五位數,etc
- 生成所有奇數位的palindrome,如果它比N大且是prime,返回
class Solution {
public int primePalindrome(int N) {
if (N >= 8 && N <= 11) return 11;
for (int i=1; i<100000; i++) {
String x = Integer.toString(i);
String y = new StringBuilder(x).reverse().toString().substring(1);
int out = Integer.parseInt(x + y);
if (out >= N && isPrime(out)) return out;
}
return -1;
}
public boolean isPrime(int x) {
if (x < 2 || x % 2 == 0) return x == 2;
for (int i = 3; i * i <= x; i += 2)
if (x % i == 0) return false;
return true;
}
}
276 Palindrome Permutation II
- Sort the array "int[] nums" to make sure we can skip the same value.
- When a number has the same value with its previous, we can use this number only if his previous is used
private void backtracking(List<String> res, String path, String base, boolean[] used, String mid) {
if (path.length() == base.length()) {
String toAdd = path + mid + new StringBuilder(path).reverse().toString();
res.add(toAdd);
}
for (int i=0; i<base.length(); i++) {
if (used[i]) continue;
if (i > 0 && base.charAt(i) == base.charAt(i-1) && !used[i-1]) continue;
used[i] = true;
path += Character.toString(base.charAt(i));
backtracking(res, path, base, used, mid);
used[i] = false;
path = path.substring(0, path.length() - 1);
}
}
131 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.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]
- DP + DFS
- Use a 2d array to keep track of any string we have scanned so far, with an addition pair, we can determine whether it's palindrome or not by justing looking at that pair, which is this line if(s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1]))
. This way, the 2d array dp contains the possible palindrome partition among all. - O(n^2)把整個string掃一遍,用簡單的backtracking找到滿足條件的解
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
boolean[][] dp = new boolean[s.length()][s.length()];
/* dp[i][j] records whether s[j:i+1] is a palindrome*/
for (int i=0; i<s.length(); i++) {
for (int j=0; j<=i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[i-1][j+1])) dp[i][j] = true;
}
}
backtracking(res, dp, new ArrayList<String>(), s, 0);
return res;
}
private void backtracking(List<List<String>> res, boolean[][] dp, List<String> path, String s, int index) {
if (index == s.length()) {
res.add(new ArrayList<String>(path));
return;
}
for (int i=index; i<s.length(); i++) {
if (dp[i][index]) {
path.add(s.substring(index, i+1));
backtracking(res, dp, path, s, i+1);
path.remove(path.size() - 1);
}
}
}
}