We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e8ee61d commit 2e85a37Copy full SHA for 2e85a37
word-break/TonyKim9401.java
@@ -0,0 +1,22 @@
1
+// TC: O(n^2)
2
+// -> use 2 for-loops to search
3
+// SC: O(n)
4
+// -> boolean array's size
5
+class Solution {
6
+ public boolean wordBreak(String s, List<String> wordDict) {
7
+ Set<String> set = new HashSet(wordDict);
8
+
9
+ boolean[] dp = new boolean[s.length() + 1];
10
+ dp[0] = true;
11
12
+ for (int i = 1; i <= s.length(); i++) {
13
+ for (int j = 0; j < i; j++) {
14
+ if (dp[j] && set.contains(s.substring(j, i))) {
15
+ dp[i] = true;
16
+ break;
17
+ }
18
19
20
+ return dp[s.length()];
21
22
+}
0 commit comments