You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Java code summarizes solutions for leetcode string problems.
2
-
3
-
# StringSolution.java
4
-
5
-
## 139. Word Break
6
-
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
7
-
8
-
For example, given
9
-
s = "leetcode",
10
-
dict = ["leet", "code"].
11
-
12
-
Return true because "leetcode" can be segmented as "leet code".
13
-
14
-
#### Solution
15
-
Dynamic Programming.
16
-
1. One dimension array dp[], dp[i] means whether a substring from 0 to i (exclusive) has a valid word break or not.
17
-
2. Transition formula: dp[i] = (dp[0] && dict.contains(s.substring(0, i + 1))) || (dp[1] && dict.contains(s.substring(1, i + 1)))) || ... || (dp[i] && dict.contains(s.substring(i, i + 1)))).
18
-
19
-
## 140. Word Break II
20
-
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.
Use a HashMap<String, LinkedList<String>> map to store word breaks of substrings, so as to avoid duplicated search.
40
-
41
-
## 293. Flip Game (Easy)
42
-
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.
43
-
44
-
Write a function to compute all possible states of the string after one valid move.
45
-
46
-
For example, given s = "++++", after one move, it may become one of the following states:
47
-
~~~~
48
-
[
49
-
"--++",
50
-
"+--+",
51
-
"++--"
52
-
]
53
-
~~~~
54
-
If there is no valid move, return an empty list [].
55
-
56
-
#### Solution
57
-
Simply iterate the string.
58
-
59
-
## 165. Compare Version Numbers
60
-
Compare two version numbers version1 and version2.
61
-
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
62
-
63
-
You may assume that the version strings are non-empty and contain only digits and the . character.
64
-
The . character does not represent a decimal point and is used to separate number sequences.
65
-
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
66
-
67
-
Here is an example of version numbers ordering:
68
-
~~~~
69
-
0.1 < 1.1 < 1.2 < 13.37
70
-
~~~~
71
-
72
-
#### Solution
73
-
WRONG: my first idea is to compare v1 and v2 at their min length; and then handle the one that is longer. However, it becomes trivial to handle corner case, like "1.0" v.s. "1", and "01" v.s. "1".
74
-
75
-
~~~~
76
-
for (int i = 0; i < Math.min(tokens1.length, tokens2.length); i++) {
77
-
int v1 = Integer.parseInt(tokens1[i]);
78
-
int v2 = Integer.parseInt(tokens2[i]);
79
-
if (v1 != v2) {
80
-
return v1 > v2 ? 1 : -1;
81
-
}
82
-
}
83
-
~~~~
84
-
85
-
Think from the other side, compare at their max length, for the one which is shorter, simply set it to 0.
0 commit comments