Skip to content

Commit 3c33ef0

Browse files
committed
3.13.2016
1 parent ac147c9 commit 3c33ef0

File tree

5 files changed

+329
-122
lines changed

5 files changed

+329
-122
lines changed

Java/Word Ladder.java

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
M
2+
3+
Brutle: 遍历所有26个字母
4+
5+
方法2:
6+
用Trie理应更快. However implementation可能有点重复计算的地方LeetCode timeout. 需要再做做
7+
8+
```
19
/*
210
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
311
@@ -124,3 +132,183 @@ else if (i == str.length() - 1) {
124132
}//END FOR I
125133
}
126134
}
135+
136+
137+
138+
139+
140+
141+
142+
/*
143+
Use a new method in Trie:
144+
getChildrenMap(string s): get the children hashmap at last char of s.
145+
getCurrMap(s): get the hashmap where the last char of s is at.
146+
147+
However, still timeout in LeetCode. So there might be some case that's using extra calculation.
148+
It should ideally be faster than brutle force.
149+
*/
150+
import java.io.*;
151+
import java.util.*;
152+
153+
class Solution {
154+
155+
class TrieNode {
156+
String str;
157+
boolean isEnd;
158+
boolean visited;
159+
HashMap<Character, TrieNode> children;
160+
public TrieNode () {
161+
this.isEnd = false;
162+
this.visited = false;
163+
this.str = "";
164+
children = new HashMap<Character, TrieNode>();
165+
}
166+
}
167+
168+
169+
170+
public TrieNode root = new TrieNode();
171+
public int leng = Integer.MAX_VALUE;
172+
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
173+
if (beginWord == null || endWord == null || beginWord.length() != endWord.length()
174+
|| wordList == null || wordList.size() == 0) {
175+
return 0;
176+
}
177+
178+
//build Trie
179+
for (String s : wordList) {
180+
insert(s);
181+
}
182+
dfs(beginWord, endWord, 1);
183+
184+
return leng;
185+
}
186+
187+
public void dfs(String start, String end, int step) {
188+
if (step >= leng) {
189+
return;
190+
}
191+
192+
if (compare(start, end) == 0) {
193+
leng = Math.min(leng, step);
194+
return;
195+
}
196+
//catch last step, so it covers the case where last change is not in dictionary
197+
if (compare(start, end) == 1) {
198+
leng = Math.min(leng, step + 1);
199+
return;
200+
}
201+
202+
for (int i = 0; i < start.length(); i++) {//try to replace every index
203+
String s = start.substring(0, i + 1);
204+
HashMap<Character, TrieNode> candidates = getCurrentMap(s);
205+
206+
//If char(i) not in dictinary, go back up one level,
207+
//try to find all possible children candidates of previous char. Keep going with the process
208+
if (candidates == null) {
209+
s = start.substring(0, i);
210+
candidates = getChildrenMap(s);
211+
//If prev char is not found neither, fail it.
212+
//Example, when 'b' in "ab" not found, try to find 'a' and its children; however, if 'a' not found neither, cut it off here
213+
if (candidates == null) {
214+
continue;
215+
}
216+
}
217+
218+
//Replace char at i with all candidates existing in Trie dictionary
219+
for (Map.Entry<Character, TrieNode> entry : candidates.entrySet()) {
220+
char c = entry.getKey();
221+
String newStr = start.substring(0,i) + c + start.substring(i+1);
222+
if (c != start.charAt(i) && !entry.getValue().visited) {
223+
candidates.get(c).visited = true;
224+
dfs(newStr, end, step + 1);
225+
}
226+
}
227+
}
228+
}
229+
230+
/**************************Trie section *********************/
231+
//In this problem ,didin't use startWith() or search().
232+
//Instead, use a similar function, getChildrenMap. See below
233+
234+
public void insert (String s){
235+
char[] arr = s.toCharArray();
236+
TrieNode node = root;
237+
for (int i = 0; i < arr.length; i++) {
238+
char c = arr[i];
239+
if (!node.children.containsKey(c)) {
240+
node.children.put(c, new TrieNode());
241+
}
242+
node = node.children.get(c);
243+
if (i == arr.length - 1) {
244+
node.isEnd = true;
245+
node.str = s;
246+
}
247+
}
248+
}
249+
250+
/*Return the HashMap where the last char of s is in*/
251+
public HashMap<Character, TrieNode> getCurrentMap(String s) {
252+
char[] arr = s.toCharArray();
253+
TrieNode node = root;
254+
for (int i = 0; i < arr.length; i++) {
255+
char c = arr[i];
256+
if (!node.children.containsKey(c)) {
257+
return null;
258+
}
259+
if (i == arr.length - 1) {
260+
return node.children;
261+
}
262+
node = node.children.get(c);
263+
}
264+
return null;
265+
}
266+
267+
/*Return the children HashMap of the last char*/
268+
public HashMap<Character, TrieNode> getChildrenMap(String s) {
269+
if (s == null || s.length() == 0) {
270+
return root.children;
271+
}
272+
char[] arr = s.toCharArray();
273+
TrieNode node = root;
274+
for (int i = 0; i < arr.length; i++) {
275+
char c = arr[i];
276+
if (!node.children.containsKey(c)) {
277+
return null;
278+
}
279+
node = node.children.get(c);
280+
}
281+
return node.children;
282+
}
283+
284+
//Helper to comapre string and return the difference if not <= 1
285+
public int compare(String s1, String s2) {
286+
int count = 0;
287+
for (int i = 0; i < s1.length(); i++) {
288+
count += s1.charAt(i) != s2.charAt(i) ? 1 : 0;
289+
if (count > 1) {
290+
return count;
291+
}
292+
}
293+
return count;
294+
}
295+
296+
297+
public static void main(String[] args) {
298+
String beginWord = "hit";//"hit";
299+
String endWord = "cog";
300+
Set<String> set = new HashSet<String>();
301+
set.add("hot"); set.add("dot"); set.add("dog"); set.add("lot"); set.add("log");
302+
303+
304+
Solution sol = new Solution();
305+
int leng = sol.ladderLength(beginWord, endWord, set);
306+
307+
System.out.println("rst " + leng );
308+
System.out.println("END");
309+
}
310+
311+
}
312+
313+
314+
```

Java/Word Search II.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,11 @@ public boolean search(char[][] board, String word, int i, int j, int start) {
270270
}
271271
}
272272

273+
274+
275+
276+
277+
278+
279+
273280
```

Java/Word Search.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Backtracking方法2:
99
用一个boolean visited[][]
1010

11+
12+
1113
```
1214
/*
1315
Given a 2D board and a word, find if the word exists in the grid.
@@ -139,4 +141,8 @@ public boolean dfs (int index, int x, int y, boolean[][] visited, char[][] board
139141
}
140142
}
141143

144+
145+
146+
147+
142148
```

Others/MorseCode.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
想法:
2+
1. 只关注正数找一个分割点开始查看能不能有valid的解
3+
2. 过程中把正数可能有的所有正数的解都找出来可能存为一个ArrayList<String> letters in morse format:
4+
String "... --- ..." 每个字母之间有空格分开
5+
这里可能生成所有possibilities其中有很多possibility在结合了负数后可能就会不成立
6+
7+
3. loop through arraylist: letters
8+
根据每个字母以及每个字母后面的停顿来尝试跟所有的负数分段比较
9+
比如第一个字母:[. -x . -y . -z] z 肯定比x 和y都大而SOS后面的空出来的负数时间应该比这个词里面的所有负数绝对值都大
10+
11+
这样就把ArrayList Letters 里面的很多可能性都灭掉出一个或者多个结果~
12+
13+
14+
15+
当然这里负数只做了最终判断的作用而一开始正数出的结果可能会非常耗时
16+
而且假设了分段判断的规律而不假设全局的规律。(好像更切合实际)。
17+
18+
19+
```
20+
//Trie
21+
22+
23+
import java.io.*;
24+
import java.util.*;
25+
26+
/*
27+
* To execute Java, please define "static void main" on a class
28+
* named Solution.
29+
*
30+
* If you need more classes, simply define them inline.
31+
*/
32+
33+
34+
/*
35+
Double[] = {0.15, -0.12, 0.16, -0.1, 0.17, -0.3}
36+
pos: . - time
37+
neg: empty time
38+
39+
morseCodeDict["A"] = ".-"
40+
morseCodeDict["B"] = "-..."
41+
morseCodeDict["C"] = "-.-."
42+
morseCodeDict["D"] = "-.."
43+
morseCodeDict["E"] = "."
44+
morseCodeDict["F"] = "..-."
45+
morseCodeDict["G"] = "--."
46+
morseCodeDict["H"] = "...."
47+
morseCodeDict["I"] = ".."
48+
morseCodeDict["J"] = ".---"
49+
morseCodeDict["K"] = "-.-"
50+
morseCodeDict["L"] = ".-.."
51+
morseCodeDict["M"] = "--"
52+
morseCodeDict["N"] = "-."
53+
morseCodeDict["O"] = "---"
54+
morseCodeDict["P"] = ".--."
55+
morseCodeDict["Q"] = "--.-"
56+
morseCodeDict["R"] = ".-."
57+
morseCodeDict["S"] = "..."
58+
morseCodeDict["T"] = "-"
59+
morseCodeDict["U"] = "..-"
60+
morseCodeDict["V"] = "...-"
61+
morseCodeDict["W"] = ".--"
62+
morseCodeDict["X"] = "-..-"
63+
morseCodeDict["Y"] = "-.--"
64+
morseCodeDict["Z"] = "--.."
65+
66+
morseCodeDict["1"] = "-----"
67+
morseCodeDict["2"] = ".----"
68+
morseCodeDict["3"] = "..---"
69+
morseCodeDict["4"] = "...--"
70+
morseCodeDict["5"] = "....-"
71+
morseCodeDict["6"] = "....."
72+
morseCodeDict["7"] = "-...."
73+
morseCodeDict["8"] = "--..."
74+
morseCodeDict["9"] = "---.."
75+
morseCodeDict["0"] = "----."
76+
77+
Example:
78+
[0.0450931929901705, -0.0901581814005595, 0.0678896922281069, -0.121032138856854, 0.152995205350165, -0.276777733213167, 0.275236261113044, -0.0919186682910514, 0.347645293572589, -0.125459771265131, 0.342321600880377, -0.363701690087054, 0.102393076997638, -0.117040938252157, 0.114223775510942, -0.0895479217582997, 0.0973858877985071] = SOS ...---...
79+
80+
Thoughts:
81+
Pos:
82+
Two sets: dot or dash.
83+
Negative:
84+
Three sets: time between morse code, time between letter, time between words.
85+
86+
Pick a pos time for '.', pick a negtive time for time between morse code, then start DFS.
87+
88+
For initial judgement:
89+
Pos:
90+
Sort all postive number, let the smallest be '.' and the rest to be '-'. try it out. If not working, move the threshold.
91+
92+
Negative:
93+
Sort by absolute value.
94+
Assume all letters are the shortest one morse code. So the set for 'time between word' will have most of number.
95+
The set for 'time between letter' will be empty
96+
The set for 'time between morse code' will be empty
97+
If not working, move number around the tree sets, and do dfs.
98+
99+
The possible solution is to try all posibility with dfs, and cut it off when it seems not valid.
100+
101+
102+
103+
Thought2:
104+
Ignore negative for now. Try to use same method for positive number to try out all posibilities with postiive numbers.
105+
Use the results to match negative numbers, see if the time pulse is valid.
106+
If match, return all posibilities.
107+
108+
*/
109+
class Solution {
110+
public static void main(String[] args) {
111+
ArrayList<String> strings = new ArrayList<String>();
112+
strings.add("Hello, World!");
113+
strings.add("Welcome to CoderPad.");
114+
strings.add("This pad is running Java 8.");
115+
116+
for (String string : strings) {
117+
System.out.println(string);
118+
}
119+
}
120+
}
121+
122+
```

0 commit comments

Comments
 (0)