Skip to content

Commit d856b3b

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 30a7a11 commit d856b3b

6 files changed

+584
-2
lines changed

leetcode/336. Palindrome Pairs.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
## 336. Palindrome Pairs
2+
3+
### Question
4+
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.
5+
6+
```
7+
Example 1:
8+
9+
Input: ["abcd","dcba","lls","s","sssll"]
10+
Output: [[0,1],[1,0],[3,2],[2,4]]
11+
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]
12+
13+
Example 2:
14+
15+
Input: ["bat","tab","cat"]
16+
Output: [[0,1],[1,0]]
17+
Explanation: The palindromes are ["battab","tabbat"]
18+
```
19+
20+
### Thinking:
21+
* Method 1: Brutal force TLE
22+
```Java
23+
class Solution {
24+
public List<List<Integer>> palindromePairs(String[] words) {
25+
int len = words.length;
26+
List<List<Integer>> res = new ArrayList<>();
27+
for(int i = 0; i < len; i++){
28+
for(int j = 0; j < len; j++){
29+
if(i == j) continue;
30+
if(isPalindrome(words[i] + words[j])){
31+
List<Integer> temp = new ArrayList<>();
32+
temp.add(i);
33+
temp.add(j);
34+
res.add(temp);
35+
}
36+
}
37+
}
38+
return res;
39+
}
40+
private boolean isPalindrome(String s){
41+
int left = 0, right = s.length() - 1;
42+
char[] arr = s.toCharArray();
43+
while(left < right){
44+
if(arr[left++] != arr[right--])
45+
return false;
46+
}
47+
return true;
48+
}
49+
}
50+
```
51+
52+
* Method 2: HashMap O(N * K^2)
53+
```Java
54+
class Solution {
55+
public List<List<Integer>> palindromePairs(String[] words) {
56+
List<List<Integer>> res = new ArrayList<>();
57+
if(words == null || words.length < 2) return res;
58+
Map<String, Integer> map = new HashMap<>();
59+
for(int i = 0; i < words.length; i++){ // O(N)
60+
map.put(words[i], i);
61+
}
62+
for(int i = 0; i < words.length; i++){ // O(N * k)
63+
for(int j = 0; j <= words[i].length(); j++){
64+
String first = words[i].substring(0, j);
65+
String second = words[i].substring(j);
66+
if(isPalindrome(first)){ // O(k)
67+
//first is palindrome, check if we have reverse of second
68+
String reverse = new StringBuilder(second).reverse().toString();
69+
if(map.containsKey(reverse) && map.get(reverse) != i){
70+
List<Integer> temp = new ArrayList<>();
71+
temp.add(map.get(reverse));
72+
temp.add(i);
73+
res.add(temp);
74+
}
75+
}
76+
if(second.length() != 0 && isPalindrome(second)){ // O(k)
77+
String reverse = new StringBuilder(first).reverse().toString();
78+
if(map.containsKey(reverse) && map.get(reverse) != i){
79+
List<Integer> temp = new ArrayList<>();
80+
temp.add(i);
81+
temp.add(map.get(reverse));
82+
res.add(temp);
83+
}
84+
}
85+
}
86+
}
87+
return res;
88+
}
89+
private boolean isPalindrome(String s){
90+
int left = 0, right = s.length() - 1;
91+
char[] arr = s.toCharArray();
92+
while(left < right){
93+
if(arr[left++] != arr[right--])
94+
return false;
95+
}
96+
return true;
97+
}
98+
}
99+
```
100+
101+
* Method 3: Tire Tree
102+
```Java
103+
class Solution {
104+
private static class Node{
105+
Node[] childs;
106+
boolean isLeaf;
107+
int index;
108+
public Node(){
109+
this.childs = new Node[26];
110+
}
111+
}
112+
private Node root;
113+
private void insert(String s, int index){
114+
char[] arr = s.toCharArray();
115+
Node temp = root;
116+
for(int i = 0; i < arr.length; i++){
117+
if(temp.childs[arr[i] - 'a'] == null){
118+
temp.childs[arr[i] - 'a'] = new Node();
119+
}
120+
temp = temp.childs[arr[i] - 'a'];
121+
}
122+
temp.isLeaf = true;
123+
temp.index = index;
124+
}
125+
private Node contains(String s){
126+
char[] arr = s.toCharArray();
127+
Node temp = root;
128+
for(int i = 0; i < arr.length; i++){
129+
if(temp.childs[arr[i] - 'a'] == null) return null;
130+
temp = temp.childs[arr[i] - 'a'];
131+
}
132+
return temp;
133+
}
134+
public List<List<Integer>> palindromePairs(String[] words) {
135+
this.root = new Node();
136+
for(int i = 0; i < words.length; i++){
137+
insert(words[i], i);
138+
}
139+
List<List<Integer>> result = new ArrayList<>();
140+
for(int i = 0; i < words.length; i++){
141+
for(int j = 0; j <= words[i].length(); j++){
142+
String first = words[i].substring(0, j);
143+
String second = words[i].substring(j);
144+
if(isPalindrome(first)){
145+
String reverse = new StringBuilder(second).reverse().toString();
146+
Node temp = contains(reverse);
147+
if(temp != null && temp.isLeaf && temp.index != i){
148+
result.add(Arrays.asList(temp.index, i));
149+
}
150+
}
151+
if(second.length() != 0 && isPalindrome(second)){
152+
String reverse = new StringBuilder(first).reverse().toString();
153+
Node temp = contains(reverse);
154+
if(temp != null && temp.isLeaf && temp.index != i){
155+
result.add(Arrays.asList(i, temp.index));
156+
}
157+
}
158+
}
159+
}
160+
return result;
161+
}
162+
private boolean isPalindrome(String s){
163+
int l = 0, r = s.length() - 1;
164+
while(l < r){
165+
if(s.charAt(l++) != s.charAt(r--)) return false;
166+
}
167+
return true;
168+
}
169+
}
170+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## 438. Find All Anagrams in a String
2+
3+
### Question
4+
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
5+
6+
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
7+
8+
The order of output does not matter.
9+
10+
```
11+
Example 1:
12+
13+
Input:
14+
s: "cbaebabacd" p: "abc"
15+
16+
Output:
17+
[0, 6]
18+
19+
Explanation:
20+
The substring with start index = 0 is "cba", which is an anagram of "abc".
21+
The substring with start index = 6 is "bac", which is an anagram of "abc".
22+
23+
Example 2:
24+
25+
Input:
26+
s: "abab" p: "ab"
27+
28+
Output:
29+
[0, 1, 2]
30+
31+
Explanation:
32+
The substring with start index = 0 is "ab", which is an anagram of "ab".
33+
The substring with start index = 1 is "ba", which is an anagram of "ab".
34+
The substring with start index = 2 is "ab", which is an anagram of "ab".
35+
```
36+
37+
38+
### Thinking:
39+
* Method 1: HashMap
40+
```Java
41+
class Solution {
42+
public List<Integer> findAnagrams(String s, String p) {
43+
Map<Character, Integer> map = new HashMap<>();
44+
for(char c : p.toCharArray()){
45+
map.put(c, map.getOrDefault(c, 0) + 1);
46+
}
47+
int pLen = p.length(), sLen = s.length();
48+
List<Integer> res = new ArrayList<>();
49+
if(sLen < pLen) return res;
50+
Map<Character, Integer> temp = new HashMap<>();
51+
for(int i = 0; i < pLen; i++){
52+
char c = s.charAt(i);
53+
temp.put(c, temp.getOrDefault(c, 0) + 1);
54+
}
55+
for(int i = pLen; i <= sLen; i++){
56+
if(check(temp, map)) res.add(i - pLen);
57+
// Remove the first character
58+
char first = s.charAt(i - pLen);
59+
Integer count = temp.get(first);
60+
if(count == 1) temp.remove(first);
61+
else if(count != null) temp.put(first, count - 1);
62+
if(i < sLen){
63+
char c = s.charAt(i);
64+
temp.put(c, temp.getOrDefault(c, 0) + 1);
65+
}
66+
}
67+
return res;
68+
}
69+
private boolean check(Map<Character, Integer> map1, Map<Character, Integer> map2){
70+
for(Map.Entry<Character, Integer> entry : map1.entrySet()){
71+
if(!map2.containsKey(entry.getKey())) return false;
72+
if(!map2.get(entry.getKey()).equals(entry.getValue())) return false;
73+
}
74+
return true;
75+
}
76+
}
77+
```
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## 710. Random Pick with Blacklist
2+
3+
### Question
4+
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform random integer from [0, N) which is NOT in B.
5+
6+
Optimize it such that it minimizes the call to system’s Math.random().
7+
8+
Note:
9+
1. 1 <= N <= 1000000000
10+
2. 0 <= B.length < min(100000, N)
11+
3. [0, N) does NOT include N. See interval notation.
12+
13+
```
14+
Example 1:
15+
16+
Input:
17+
["Solution","pick","pick","pick"]
18+
[[1,[]],[],[],[]]
19+
Output: [null,0,0,0]
20+
21+
Example 2:
22+
23+
Input:
24+
["Solution","pick","pick","pick"]
25+
[[2,[]],[],[],[]]
26+
Output: [null,1,1,1]
27+
28+
Example 3:
29+
30+
Input:
31+
["Solution","pick","pick","pick"]
32+
[[3,[1]],[],[],[]]
33+
Output: [null,0,0,2]
34+
35+
Example 4:
36+
37+
Input:
38+
["Solution","pick","pick","pick"]
39+
[[4,[2]],[],[],[]]
40+
Output: [null,1,3,1]
41+
```
42+
43+
Explanation of Input Syntax:
44+
45+
The input is two lists: the subroutines called and their arguments. Solution's constructor has two arguments, N and the blacklist B. pick has no arguments. Arguments are always wrapped with a list, even if there aren't any.
46+
47+
48+
### Thinking:
49+
* Method 1: TLE / MLE
50+
```Java
51+
class Solution {
52+
private Set<Integer> black;
53+
private int[] valid;
54+
private Random random;
55+
private int N;
56+
private int size;
57+
public Solution(int N, int[] blacklist) {
58+
this.black = new HashSet<>();
59+
for(int b : blacklist) this.black.add(b);
60+
this.valid = new int[N - blacklist.length];
61+
int count = 0;
62+
for(int i = 0; i < N; i++){
63+
if(this.black.contains(i)) continue;
64+
this.valid[count++] = i;
65+
}
66+
this.size = this.valid.length;
67+
this.random = new Random();
68+
this.N = N;
69+
}
70+
71+
public int pick() {
72+
return this.valid[Math.abs(random.nextInt()) % size];
73+
}
74+
}
75+
76+
/**
77+
* Your Solution object will be instantiated and called as such:
78+
* Solution obj = new Solution(N, blacklist);
79+
* int param_1 = obj.pick();
80+
*/
81+
```
82+
83+
* Method 2: reorder HashMap
84+
1. We first create a hashtable for saving all blacklist.
85+
2. we insert all the valid numbers to previous blacklist positions.
86+
3. Use random to get a random value, if that value is in the map, we return the value, otherwise, we just return the value directly.
87+
```Java
88+
class Solution {
89+
private Map<Integer, Integer> map;
90+
private Random random;
91+
private int M;
92+
public Solution(int N, int[] blacklist) {
93+
this.random = new Random();
94+
this.map = new HashMap<>();
95+
for(int b : blacklist){
96+
map.put(b, -1);
97+
}
98+
this.M = N - blacklist.length;
99+
N--;
100+
for(int b : blacklist){
101+
if(b < M){
102+
while(map.containsKey(N)) N--;
103+
map.put(b, N--);
104+
}
105+
}
106+
}
107+
108+
public int pick() {
109+
int next = random.nextInt(M);
110+
return map.containsKey(next) ? map.get(next): next;
111+
}
112+
}
113+
114+
/**
115+
* Your Solution object will be instantiated and called as such:
116+
* Solution obj = new Solution(N, blacklist);
117+
* int param_1 = obj.pick();
118+
*/
119+
```

0 commit comments

Comments
 (0)