This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// O(n^2) O(n) | |
public String shortestPalindrome(String s) { | |
int n = s.length(); | |
String rev = new StringBuilder(s).reverse().toString(); | |
int j = 0; | |
for (int i = 0; i < n; i++) { | |
if (s.substring(0, n-i).equals(rev.substring(i))) | |
return rev.substring(0, i) + s; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isPalindrome(String s) { | |
if (s.length() == 0) return true; | |
StringBuilder validLetters = new StringBuilder(); | |
for (Character c : s.toCharArray()) { | |
if (Character.isLetter(c) || Character.isDigit(c)) { | |
validLetters.append(Character.toLowerCase(c)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isPalindrome(int x) { | |
if (x < 0 || (x!=0 && x%10==0)) return false; | |
int rev = 0; | |
while (x > rev) { | |
rev = rev * 10 + x % 10; | |
x = x/10; | |
} | |
return (x == rev || x == rev/10); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public static String longestPalindrome(String s) { | |
if (s == null) return null; | |
if (s.length() <= 1) return s; | |
int start = 0; | |
int end = 0; | |
// foreach find longest | |
for (int i = 0; i < s.length(); i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// O(n) O(1) | |
public boolean isPalindrome(ListNode head) { | |
ListNode rev = null, tmp = null; | |
ListNode fast = head; | |
while (fast != null && fast.next != null) { | |
fast = fast.next.next; | |
tmp = rev; | |
rev = head; | |
head = head.next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private List<String> list = new ArrayList<>(); | |
public List<String> generatePalindromes(String s) { | |
int numOdds = 0; // How many characters that have odd number of count | |
int[] map = new int[256]; // Map from character to its frequency | |
for (char c: s.toCharArray()) { | |
map[c]++; | |
numOdds = (map[c] & 1) == 1 ? numOdds+1 : numOdds-1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// O(n) O(n) | |
public boolean canPermutePalindrome(String s) { | |
HashSet<Character> set = new HashSet<Character>(); | |
for (Character c : s.toCharArray()) { | |
if (!set.add(c)) { | |
set.remove(c); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// O(n^2) O(1) | |
public int largestPalindrome(int n) { | |
if (n == 1) return 9; | |
int max = (int) Math.pow(10, n) - 1; | |
for (int v = max-1; v > max/10; v--) { | |
long u=Long.valueOf(v+new StringBuilder().append(v).reverse().toString()); | |
for (long x = max; x*x > u; x--) { | |
if (u % x == 0) return (int) (u%1337); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// O(n) O(1) | |
public int longestPalindrome(String s) { | |
if (s.length() == 0 || s == null) return 0; | |
int[] counts = new int[52]; | |
for (Character c : s.toCharArray()) { | |
int idx = 0; | |
if (c.compareTo('a') < 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> palindromePairs(String[] words) { | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
if(words == null || words.length == 0){ | |
return res; | |
} | |
//build the map save the key-val pairs: String - idx | |
HashMap<String, Integer> map = new HashMap<>(); | |
for(int i = 0; i < words.length; i++){ | |
map.put(words[i], i); |