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 }
  • 相关阅读:
    3.17JSP作业
    JSP第二次作业
    JSP第一次作业
    软件测试课堂练习
    增删改查
    登录界面
    购物商城
    图床
    JSP-2020年4月14日-第七周
    JSP-2020年4月12日-第六周
  • 原文地址:https://www.cnblogs.com/jasonC/p/3436754.html
Copyright © 2011-2022 走看看