zoukankan      html  css  js  c++  java
  • Substring with Concatenation of All Words

     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 }
  • 相关阅读:
    仓鼠找sugar(LCA)
    bzoj4481非诚勿扰(期望dp)
    NOIP2011Mayan游戏(模拟)
    [国家集训队]旅游
    NOIP2012疫情控制(二分答案+树上贪心)
    NOIP2017题解
    [SCOI2010]幸运数字(容斥+爆搜)
    [JSOI2008]Blue Mary的战役地图(二分+哈希)
    [湖南集训]谈笑风生(主席树)
    NOIP2016题解
  • 原文地址:https://www.cnblogs.com/jasonC/p/3436754.html
Copyright © 2011-2022 走看看