|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.HashMap; |
5 | 6 | import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.stream.Collectors; |
6 | 10 |
|
7 | 11 | /** |
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. |
12 | 16 |
|
13 | 17 | For example, given: |
14 | 18 | s: "barfoothefoobarman" |
|
19 | 23 | */ |
20 | 24 | public class _30 { |
21 | 25 |
|
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 | + } |
26 | 45 | } |
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 | + } |
36 | 52 | } |
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); |
74 | 55 | } |
75 | | - return res; |
| 56 | + } |
| 57 | + return result; |
76 | 58 | } |
| 59 | + } |
77 | 60 |
|
78 | 61 | } |
0 commit comments