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 }
  • 相关阅读:
    Win10使用VMware虚拟机安装ubuntu
    算法资源清单
    JAVA Synchronized (三) volatile 与 synchronized 的比较
    JAVA Synchronized (二)
    Java多线程:线程状态以及wait(), notify(), notifyAll()
    Java中断机制
    Java throw与throws
    Java(Android)线程池
    JAVA interrupte中断线程的真正用途
    Java 守护线程
  • 原文地址:https://www.cnblogs.com/jasonC/p/3436754.html
Copyright © 2011-2022 走看看