Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 214_0914.java
Last active September 14, 2017 20:57
[214. Shortest Palindrome] #leetcode
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;
@cixuuz
cixuuz / 125_0914.java
Created September 14, 2017 18:47
[125. Valid Palindrome] #leetcode
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));
}
}
@cixuuz
cixuuz / 9_0914.java
Created September 14, 2017 17:32
[9. Palindrome Number] #leetcode
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);
}
@cixuuz
cixuuz / 5_0914.java
Created September 14, 2017 17:23
[5. Longest Palindromic Substring] #leetcode
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++) {
@cixuuz
cixuuz / 234_0914.java
Created September 14, 2017 16:13
[234. Palindrome Linked List] #leetcode
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;
@cixuuz
cixuuz / 267_0913.java
Created September 14, 2017 00:36
[267. Palindrome Permutation II] #leetcode
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;
}
@cixuuz
cixuuz / 266_0913.java
Created September 13, 2017 22:09
[266. Palindrome Permutation] #leetcode
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);
}
}
@cixuuz
cixuuz / 478_0913.java
Created September 13, 2017 21:57
[479. Largest Palindrome Product] #leetcode
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);
}
@cixuuz
cixuuz / 409_0913.java
Created September 13, 2017 21:23
[409. Longest Palindrome] #leetcode
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) {
@cixuuz
cixuuz / 336_0907.java
Created September 8, 2017 02:12
[336. Palindrome Pairs] #leetcode
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);