|
| 1 | +package com.blankj.hard._030; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +/** |
| 12 | + * <pre> |
| 13 | + * author: Blankj |
| 14 | + * blog : http://blankj.com |
| 15 | + * time : 2018/02/01 |
| 16 | + * desc : |
| 17 | + * </pre> |
| 18 | + */ |
| 19 | +public class Solution { |
| 20 | + public List<Integer> findSubstring(String s, String[] words) { |
| 21 | + int wordsSize = words.length, wordLen = words[0].length(), end = s.length() - wordsSize * wordLen; |
| 22 | + if (end < 0) return Collections.emptyList(); |
| 23 | + Map<String, Integer> countMap = new HashMap<>(); |
| 24 | + for (String word : words) { |
| 25 | + countMap.put(word, countMap.getOrDefault(word, 0) + 1); |
| 26 | + } |
| 27 | + List<Integer> res = new ArrayList<>(); |
| 28 | + Set<Integer> ignores = new HashSet<>(); |
| 29 | + for (int i = 0; i <= end; ++i) { |
| 30 | + if (ignores.contains(i)) continue; |
| 31 | + Map<String, Integer> findMap = new HashMap<>(); |
| 32 | + int st = i, count = 0; |
| 33 | + List<Integer> ignore = new ArrayList<>(); |
| 34 | + for (int j = 0; ; ++j) { |
| 35 | + int cur = i + j * wordLen; |
| 36 | + if (cur + wordLen > s.length()) break; |
| 37 | + String word = s.substring(cur, cur + wordLen); |
| 38 | + if (countMap.containsKey(word)) { |
| 39 | + findMap.put(word, findMap.getOrDefault(word, 0) + 1); |
| 40 | + ++count; |
| 41 | + while (findMap.get(word) > countMap.get(word)) { |
| 42 | + ignore.add(st); |
| 43 | + String tmp = s.substring(st, st += wordLen); |
| 44 | + findMap.put(tmp, findMap.get(tmp) - 1); |
| 45 | + --count; |
| 46 | + } |
| 47 | + if (count == wordsSize) { |
| 48 | + ignore.add(st); |
| 49 | + res.add(st); |
| 50 | + String tmp = s.substring(st, st += wordLen); |
| 51 | + findMap.put(tmp, findMap.get(tmp) - 1); |
| 52 | + --count; |
| 53 | + } |
| 54 | + } else { |
| 55 | + for (int k = i; k <= cur; k += wordLen) { |
| 56 | + ignore.add(k); |
| 57 | + } |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + ignores.addAll(ignore); |
| 62 | + } |
| 63 | + return res; |
| 64 | + } |
| 65 | + |
| 66 | +// public List<Integer> findSubstring(String S, String[] L) { |
| 67 | +// List<Integer> res = new LinkedList<>(); |
| 68 | +// int N = S.length(); |
| 69 | +// int M = L.length; // *** length |
| 70 | +// int wl = L[0].length(); |
| 71 | +// Map<String, Integer> map = new HashMap<>(), curMap = new HashMap<>(); |
| 72 | +// for (String s : L) { |
| 73 | +// if (map.containsKey(s)) map.put(s, map.get(s) + 1); |
| 74 | +// else map.put(s, 1); |
| 75 | +// } |
| 76 | +// String str = null, tmp = null; |
| 77 | +// for (int i = 0; i < wl; i++) { |
| 78 | +// int count = 0; // remark: reset count |
| 79 | +// int start = i; |
| 80 | +// for (int r = i; r + wl <= N; r += wl) { |
| 81 | +// str = S.substring(r, r + wl); |
| 82 | +// if (map.containsKey(str)) { |
| 83 | +// if (curMap.containsKey(str)) curMap.put(str, curMap.get(str) + 1); |
| 84 | +// else curMap.put(str, 1); |
| 85 | +// |
| 86 | +// if (curMap.get(str) <= map.get(str)) count++; |
| 87 | +// if (count == M) { |
| 88 | +// res.add(start); |
| 89 | +// tmp = S.substring(start, start + wl); |
| 90 | +// curMap.put(tmp, curMap.get(tmp) - 1); |
| 91 | +// start += wl; |
| 92 | +// count--; |
| 93 | +// } |
| 94 | +// while (curMap.get(str) > map.get(str)) { |
| 95 | +// tmp = S.substring(start, start + wl); |
| 96 | +// curMap.put(tmp, curMap.get(tmp) - 1); |
| 97 | +// start += wl; |
| 98 | +// if (curMap.get(tmp) < map.get(tmp)) count--; |
| 99 | +// |
| 100 | +// } |
| 101 | +// } else { |
| 102 | +// curMap.clear(); |
| 103 | +// count = 0; |
| 104 | +// start = r + wl; |
| 105 | +// } |
| 106 | +// } |
| 107 | +// curMap.clear(); |
| 108 | +// } |
| 109 | +// return res; |
| 110 | +// } |
| 111 | + |
| 112 | + public static void main(String[] args) { |
| 113 | + Solution solution = new Solution(); |
| 114 | + System.out.println(solution.findSubstring("wordgoodgoodgoodbestword", new String[]{"word", "good", "best", "good"})); |
| 115 | + System.out.println(solution.findSubstring("barfoothefoobarman", new String[]{"foo", "bar"})); |
| 116 | + System.out.println(solution.findSubstring("barfoofoobarthefoobarman", new String[]{"bar", "foo", "the"})); |
| 117 | + } |
| 118 | +} |
0 commit comments