|
| 1 | +packagecom.blankj.hard._030; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.Collections; |
| 5 | +importjava.util.HashMap; |
| 6 | +importjava.util.HashSet; |
| 7 | +importjava.util.List; |
| 8 | +importjava.util.Map; |
| 9 | +importjava.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 | +publicclassSolution { |
| 20 | +publicList<Integer>findSubstring(Strings,String[]words) { |
| 21 | +intwordsSize =words.length,wordLen =words[0].length(),end =s.length() -wordsSize *wordLen; |
| 22 | +if (end <0)returnCollections.emptyList(); |
| 23 | +Map<String,Integer>countMap =newHashMap<>(); |
| 24 | +for (Stringword :words) { |
| 25 | +countMap.put(word,countMap.getOrDefault(word,0) +1); |
| 26 | + } |
| 27 | +List<Integer>res =newArrayList<>(); |
| 28 | +Set<Integer>ignores =newHashSet<>(); |
| 29 | +for (inti =0;i <=end; ++i) { |
| 30 | +if (ignores.contains(i))continue; |
| 31 | +Map<String,Integer>findMap =newHashMap<>(); |
| 32 | +intst =i,count =0; |
| 33 | +List<Integer>ignore =newArrayList<>(); |
| 34 | +for (intj =0; ; ++j) { |
| 35 | +intcur =i +j *wordLen; |
| 36 | +if (cur +wordLen >s.length())break; |
| 37 | +Stringword =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 | +Stringtmp =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 | +Stringtmp =s.substring(st,st +=wordLen); |
| 51 | +findMap.put(tmp,findMap.get(tmp) -1); |
| 52 | + --count; |
| 53 | + } |
| 54 | + }else { |
| 55 | +for (intk =i;k <=cur;k +=wordLen) { |
| 56 | +ignore.add(k); |
| 57 | + } |
| 58 | +break; |
| 59 | + } |
| 60 | + } |
| 61 | +ignores.addAll(ignore); |
| 62 | + } |
| 63 | +returnres; |
| 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 | +publicstaticvoidmain(String[]args) { |
| 113 | +Solutionsolution =newSolution(); |
| 114 | +System.out.println(solution.findSubstring("wordgoodgoodgoodbestword",newString[]{"word","good","best","good"})); |
| 115 | +System.out.println(solution.findSubstring("barfoothefoobarman",newString[]{"foo","bar"})); |
| 116 | +System.out.println(solution.findSubstring("barfoofoobarthefoobarman",newString[]{"bar","foo","the"})); |
| 117 | + } |
| 118 | +} |