Repository navigation
Expand file tree
/
Copy pathQuestions.java
More file actions
205 lines (135 loc) · 3.83 KB
/
Copy pathQuestions.java
File metadata and controls
205 lines (135 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
LeetCode: 1. Two Sum
Difficulty: Easy
Pattern: HashTable
import java.util.HashMap;
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int need = target - nums[i];
if (map.containsKey(need)) {
return new int[]{map.get(need), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
*/
/*
LeetCode: 350. Intersection of Two Arrays II
Difficulty: Easy
Pattern: HashTable
import java.util.*;
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums1) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
ArrayList<Integer> answer = new ArrayList<>();
for (int num : nums2) {
if (map.containsKey(num) && map.get(num) > 0) {
answer.add(num);
map.put(num, map.get(num) - 1);
}
}
// Convert ArrayList to int[]
int[] result = new int[answer.size()];
for (int i = 0; i < answer.size(); i++) {
result[i] = answer.get(i);
}
return result;
}
}
*/
/*
LeetCode: 205. Isomorphic Strings
Difficulty: Easy
Pattern: HashTable
import java.util.HashMap;
class Solution {
public boolean isIsomorphic(String s, String t) {
HashMap<Character, Character> mapST = new HashMap<>();
HashMap<Character, Character> mapTS = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char chS = s.charAt(i);
char chT = t.charAt(i);
if (mapST.containsKey(chS)) {
if (mapST.get(chS) != chT) {
return false;
}
} else {
mapST.put(chS, chT);
}
if (mapTS.containsKey(chT)) {
if (mapTS.get(chT) != chS) {
return false;
}
} else {
mapTS.put(chT, chS);
}
}
return true;
}
}
*/
/*
LeetCode: 290. Word Pattern
Difficulty: Easy
Pattern: HashTable
import java.util.HashMap;
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");
if (pattern.length() != words.length) {
return false;
}
HashMap<Character, String> mapPS = new HashMap<>();
HashMap<String, Character> mapSP = new HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
String word = words[i];
// Pattern -> Word
if (mapPS.containsKey(ch)) {
if (!mapPS.get(ch).equals(word)) {
return false;
}
} else {
mapPS.put(ch, word);
}
// Word -> Pattern
if (mapSP.containsKey(word)) {
if (mapSP.get(word) != ch) {
return false;
}
} else {
mapSP.put(word, ch);
}
}
return true;
}
}
*/
/*
Leetcode: 49. Group Anagrams
Difficulty: Medium
Pattern: HashTable
import java.util.*;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String word : strs) {
char[] ch = word.toCharArray();
Arrays.sort(ch);
String sortedWord = new String(ch);
if (!map.containsKey(sortedWord)) {
map.put(sortedWord, new ArrayList<>());
}
map.get(sortedWord).add(word);
}
return new ArrayList<>(map.values());
}
}
*/