|
| 1 | +package indi.ours.algorithm.leetcode.algorithms; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author wangheng |
| 10 | + * @create 2018-11-11 下午2:30 |
| 11 | + * @desc |
| 12 | + * You are given a string, s, and a list of words, words, |
| 13 | + * that are all of the same length. |
| 14 | + * 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. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * |
| 18 | + * Input: |
| 19 | + * s = "barfoothefoobarman", |
| 20 | + * words = ["foo","bar"] |
| 21 | + * Output: [0,9] |
| 22 | + * Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively. |
| 23 | + * The output order does not matter, returning [9,0] is fine too. |
| 24 | + * Example 2: |
| 25 | + * |
| 26 | + * Input: |
| 27 | + * s = "wordgoodstudentgoodword", |
| 28 | + * words = ["word","student"] |
| 29 | + * Output: [] |
| 30 | + * |
| 31 | + * |
| 32 | + **/ |
| 33 | +public class _30 { |
| 34 | + /** |
| 35 | + * |
| 36 | + * @param s |
| 37 | + * @param words |
| 38 | + * @return |
| 39 | + */ |
| 40 | + public List<Integer> findSubstring(String s, String[] words) { |
| 41 | + int length = words[0].length(); |
| 42 | + List<Integer> res = new ArrayList<>(); |
| 43 | + if (s == null || words == null || words.length == 0 || s.length() < words[0].length() * words.length) return res; |
| 44 | + Map<String, Integer> map = new HashMap(); |
| 45 | + Map<String, Integer> cur = new HashMap(); |
| 46 | + //将 数据放入到 map 中 |
| 47 | + for (String word: words) { |
| 48 | + if (!map.containsKey(word)) map.put(word, 0); |
| 49 | + map.put(word, map.get(word) + 1); |
| 50 | + } |
| 51 | + int m = words[0].length(), n = s.length(), num = words.length; |
| 52 | + for (int i = 0; i < m; i++) { |
| 53 | + int left = i, right = i, cnt = 0; |
| 54 | + cur = new HashMap(); |
| 55 | + for (; right + m <= n; right += m) { |
| 56 | + String sub = s.substring(right, right + m); |
| 57 | + if (!map.containsKey(sub)){ |
| 58 | + cur = new HashMap(); |
| 59 | + cnt = 0; |
| 60 | + left = right + m; |
| 61 | + } |
| 62 | + else { |
| 63 | + if (!cur.containsKey(sub)) { |
| 64 | + cur.put(sub, 0); |
| 65 | + } |
| 66 | + cur.put(sub, cur.get(sub) + 1); |
| 67 | + cnt++; |
| 68 | + while (cur.get(sub) > map.get(sub)) { |
| 69 | + String leftSub = s.substring(left, left + m); |
| 70 | + cur.put(leftSub, cur.get(leftSub) - 1); |
| 71 | + cnt--; |
| 72 | + left += m; |
| 73 | + } |
| 74 | + if (cnt == num) res.add(left); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return res; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * I think the following code is self-explanatory enough. |
| 84 | + * We use an unordered_map<string, int> counts to record |
| 85 | + * the expected times of each word and another unordered_map<string, int> seen to record the times we have seen. |
| 86 | + * Then we check for every possible position of i. |
| 87 | + * Once we meet an unexpected word or the times of some word is larger than its expected times, we stop the check. |
| 88 | + * If we finish the check successfully, push i to the result indexes. |
| 89 | + * |
| 90 | + * Just build a map for the words and their relative count in L. Then we traverse through S to check whether there is a match. |
| 91 | + * @param s |
| 92 | + * @param words |
| 93 | + * @return |
| 94 | + */ |
| 95 | + public List<Integer> findSubstring2(String s, String[] words) { |
| 96 | + final Map<String, Integer> counts = new HashMap<>(); |
| 97 | + for (final String word : words) { |
| 98 | + counts.put(word, counts.getOrDefault(word, 0) + 1); |
| 99 | + } |
| 100 | + final List<Integer> indexes = new ArrayList<>(); |
| 101 | + final int n = s.length(), num = words.length, len = words[0].length(); |
| 102 | + for (int i = 0; i < n - num * len + 1; i++) { |
| 103 | + final Map<String, Integer> seen = new HashMap<>(); |
| 104 | + int j = 0; |
| 105 | + while (j < num) { |
| 106 | + final String word = s.substring(i + j * len, i + (j + 1) * len); |
| 107 | + if (counts.containsKey(word)) { |
| 108 | + seen.put(word, seen.getOrDefault(word, 0) + 1); |
| 109 | + if (seen.get(word) > counts.getOrDefault(word, 0)) { |
| 110 | + break; |
| 111 | + } |
| 112 | + } else { |
| 113 | + break; |
| 114 | + } |
| 115 | + j++; |
| 116 | + } |
| 117 | + if (j == num) { |
| 118 | + indexes.add(i); |
| 119 | + } |
| 120 | + } |
| 121 | + return indexes; |
| 122 | + } |
| 123 | +} |
0 commit comments