Skip to content

Commit 7f05a9f

Browse files
committed
string
1 parent a6a4bb7 commit 7f05a9f

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package LongestConsecutiveSequence;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* User: Danyang
8+
* Date: 1/30/2015
9+
* Time: 21:48
10+
*
11+
* Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
12+
13+
For example,
14+
Given [100, 4, 200, 1, 3, 2],
15+
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
16+
17+
Your algorithm should run in O(n) complexity.
18+
*/
19+
public class Solution {
20+
/**
21+
* Algorithm: pivoting, kind of 1-D dfs
22+
* @param num
23+
* @return
24+
*/
25+
public int longestConsecutive(int[] num) {
26+
Set<Integer> nums = new HashSet<>();
27+
for(int i=0; i<num.length; i++)
28+
nums.add(num[i]);
29+
Set<Integer> visited = new HashSet<>();
30+
int gmax = 0;
31+
for(int i=0; i<num.length; i++) {
32+
if(visited.contains(num[i]))
33+
continue;
34+
visited.add(num[i]);
35+
int len = 1;
36+
int right = num[i]+1;
37+
while(!visited.contains(right) && nums.contains(right)) {
38+
visited.add(right);
39+
right++;
40+
len++;
41+
}
42+
int left = num[i]-1;
43+
while(!visited.contains(left) && nums.contains(left)) {
44+
visited.add(left);
45+
left--;
46+
len++;
47+
}
48+
gmax = Math.max(gmax, len);
49+
}
50+
return gmax;
51+
}
52+
}

src/WordLadder/Solution.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package WordLadder;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/30/2015
11+
* Time: 22:30
12+
*
13+
* Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to
14+
* end, such that:
15+
16+
Only one letter can be changed at a time
17+
Each intermediate word must exist in the dictionary
18+
For example,
19+
20+
Given:
21+
start = "hit"
22+
end = "cog"
23+
dict = ["hot","dot","dog","lot","log"]
24+
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
25+
return its length 5.
26+
27+
Note:
28+
Return 0 if there is no such transformation sequence.
29+
All words have the same length.
30+
All words contain only lowercase alphabetic characters.
31+
*/
32+
public class Solution {
33+
/**
34+
* Algorithm bfs
35+
*
36+
* Notice:
37+
* 1. TLE due to the timing of remove the string from dict;
38+
* @param start
39+
* @param end
40+
* @param dict
41+
* @return
42+
*/
43+
public int ladderLength_TLE(String start, String end, Set<String> dict) {
44+
List<String> q = new ArrayList<>();
45+
q.add(start);
46+
int level = 1;
47+
while(!q.isEmpty()) {
48+
int l = q.size();
49+
level++;
50+
51+
for(int i=0; i<l; i++) {
52+
String cur = q.get(i);
53+
dict.remove(cur);
54+
55+
for(int j=0; j<cur.length(); j++) {
56+
char[] c = cur.toCharArray();
57+
for(char char1='a'; char1<='z'; char1++) {
58+
c[j] = char1;
59+
// String next = c.toString(); // bug
60+
String next = new String(c);
61+
if(next.equals(end))
62+
return level;
63+
if(dict.contains(next))
64+
q.add(next);
65+
}
66+
}
67+
}
68+
q = q.subList(l, q.size());
69+
}
70+
return 0;
71+
}
72+
73+
public int ladderLength(String start, String end, Set<String> dict) {
74+
List<String> q = new ArrayList<>();
75+
q.add(start);
76+
dict.remove(start);
77+
int level = 1;
78+
while(!q.isEmpty()) {
79+
int l = q.size();
80+
level++;
81+
82+
for(int i=0; i<l; i++) {
83+
String cur = q.get(i);
84+
85+
for(int j=0; j<cur.length(); j++) {
86+
char[] c = cur.toCharArray();
87+
for(char char1='a'; char1<='z'; char1++) {
88+
c[j] = char1;
89+
// String next = c.toString(); // bug
90+
String next = new String(c);
91+
if(next.equals(end))
92+
return level;
93+
if(dict.contains(next)) {
94+
q.add(next);
95+
dict.remove(next);
96+
}
97+
98+
}
99+
}
100+
}
101+
q = q.subList(l, q.size());
102+
}
103+
return 0;
104+
}
105+
106+
public static void main(String[] args) {
107+
Set<String> dict = new HashSet<>();
108+
dict.add("a");
109+
dict.add("b");
110+
dict.add("c");
111+
int ret = new Solution().ladderLength("a", "c", dict);
112+
System.out.println(ret);
113+
}
114+
}

0 commit comments

Comments
 (0)