JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 public class Solution {
 2     public ArrayList<Integer> findSubstring(String S, String[] L) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         ArrayList<Integer> results = new ArrayList<Integer>();
 6         int len = L.length;
 7         if (S.length() == 0||len == 0) {
 8             results.add(0);
 9             return results;
10         }
11 
12         int M = S.length();
13         int N = L[0].length();
14         Map<String, Integer> expected = new HashMap<String, Integer>();
15         Map<String, Integer> real = new HashMap<String, Integer>();
16         for (int j = 0; j < len; j++) {
17             if (expected.get(L[j]) == null) {
18                 expected.put(L[j], 1);
19                 real.put(L[j], 0);
20             } else {
21                 expected.put(L[j], expected.get(L[j]) + 1);
22             }
23         }
24 
25         for (int i = 0; i <= (M - N * len); i++) {
26             int j = 0;
27             for (; j < len;) {
28                 String tmp = S.substring(i + j * N, i + (j + 1) * N);
29                 if (expected.get(tmp) == null) {
30                     break;
31                 } else {
32                     real.put(tmp, real.get(tmp) + 1);
33                     if (real.get(tmp) > expected.get(tmp)) {
34                         break;
35                     }
36                     j = j + 1;
37                 }
38             }
39 
40             if (j == len) {
41                 results.add(i);
42             }
43             for (int m = 0; m < len; m++) {
44                 real.put(L[m], 0);
45             }
46 
47         }
48         return results;
49     }
50 }

 

posted on 2013-11-22 02:11  JasonChang  阅读(222)  评论(0)    收藏  举报