Skip to content

Commit fe291d2

Browse files
refactor 30
1 parent fcc61ee commit fe291d2

File tree

2 files changed

+67
-55
lines changed

2 files changed

+67
-55
lines changed
Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.fishercoder.solutions;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.HashMap;
56
import java.util.List;
7+
import java.util.Map;
8+
import java.util.function.Function;
9+
import java.util.stream.Collectors;
610

711
/**
8-
* You are given a string, s, and a list of words, words,
9-
* that are all of the same length.
10-
* Find all starting indices of substring(s) in s
11-
* that is a concatenation of each word in words exactly once and without any intervening characters.
12+
* 30. Substring with Concatenation of All Words
13+
*
14+
* You are given a string, s, and a list of words, words, that are all of the same length.
15+
* Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
1216
1317
For example, given:
1418
s: "barfoothefoobarman"
@@ -19,60 +23,39 @@
1923
*/
2024
public class _30 {
2125

22-
public List<Integer> findSubstring(String S, String[] L) {
23-
ArrayList<Integer> res = new ArrayList();
24-
if (S == null || L == null || S.length() == 0 || L.length == 0) {
25-
return res;
26+
public static class Solution1 {
27+
public List<Integer> findSubstring(String s, String[] words) {
28+
Map<String, Boolean> map = new HashMap<>();
29+
for (String word : words) {
30+
map.put(word, true);
31+
}
32+
List<Integer> result = new ArrayList<>();
33+
int startIndex = 0;
34+
for (int i = 0; i < s.length(); i++) {
35+
startIndex = i;
36+
Map<String, Boolean> clone = new HashMap<>(map);
37+
for (int j = i +1; j < s.length(); j++) {
38+
String word = s.substring(i, j);
39+
if (clone.containsKey(word) && clone.get(word)) {
40+
clone.put(word, false);
41+
i = j+1;
42+
} else {
43+
break;
44+
}
2645
}
27-
28-
HashMap<String, Integer> map = new HashMap();
29-
for (int i = 0; i < L.length; i++) {
30-
Integer val = map.get(L[i]);
31-
if (val == null) {
32-
map.put(L[i], 1);
33-
} else {
34-
map.put(L[i], val + 1);
35-
}
46+
boolean all = true;
47+
for (String word : clone.keySet()) {
48+
if (clone.get(word)) {
49+
all = false;
50+
break;
51+
}
3652
}
37-
HashMap<String, Integer> original = new HashMap();
38-
original = (HashMap<String, Integer>) map.clone();
39-
40-
/* we use two start pointers, "start" means the real starting point,
41-
* "tempStart" means the currently starting point for comparing, if one whole concatenation substring
42-
* is found, then we assign (tempStart + wordLen) to start, otherwise, start++. */
43-
int start = 0;
44-
int tempStart = 0;
45-
int wordLen = L[0].length();
46-
int wholeWordLen = wordLen * L.length;
47-
for (; start <= S.length() - wholeWordLen; start++) {
48-
map.clear();
49-
map = (HashMap<String, Integer>) original.clone();
50-
for (tempStart = start; tempStart < S.length(); tempStart += wordLen) {
51-
/* assign start to tempStart, this is a very smart way of coding, learn and master it! */
52-
if (map.size() == 0) {
53-
break;
54-
}
55-
if (tempStart + wordLen > S.length()) {
56-
break;
57-
}
58-
String sub = S.substring(tempStart, tempStart + wordLen);
59-
Integer val = map.get(sub);
60-
61-
if (val == null) {
62-
break;
63-
} else {
64-
if (val == 1) {
65-
map.remove(sub);
66-
} else {
67-
map.put(sub, val - 1);
68-
}
69-
}
70-
}
71-
if (map.size() == 0) {
72-
res.add(start);
73-
}
53+
if (all) {
54+
result.add(startIndex);
7455
}
75-
return res;
56+
}
57+
return result;
7658
}
59+
}
7760

7861
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._30;
4+
import com.fishercoder.solutions._735;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _30Test {
14+
private static _30.Solution1 solution1;
15+
private static String[] words;
16+
private static List<Integer> expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _30.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
words = new String[] {"foo", "bar"};
26+
expected = Arrays.asList(0, 9);
27+
assertEquals(expected, solution1.findSubstring("barfoothefoobarman", words));
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy