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

    You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.

    For example, given:
    S: "barfoothefoobarman"
    L: ["foo", "bar"]

    You should return the indices: [0,9].
    (order does not matter).

    Analysis:

    For a chain starting at position i, we will chase it to the end of S and put every word on this chain into a list one by one. We record the number of a word appears in array L and store them as dict. If a word is not in the dict, then the whole word list is cleared. If a word is in the dict but the number of this word appeared in the list has already be the maximum, we then remove the head of the word list until we remove this word once. If after adding some word into the list, the number of words in the list equals to the length of the array L, we then find a starting postion. In addition, we need to check such chain starting at 0..wordLen-1.

    Solution:

     1 public class Solution {
     2     public List<Integer> findSubstring(String S, String[] L) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         if (S.isEmpty() || L.length==0 || S.length()<L[0].length()) return res;
     5 
     6         for (int i=0;i<L[0].length();i++)
     7             findSubstringSub(S,L,i,res);
     8    
     9         return res;
    10     }
    11     
    12     public void findSubstringSub(String S, String[] L, int start, List<Integer> res){
    13         Map<String,Integer> wordSet = new HashMap<String,Integer>();
    14         List<String> wordList = new LinkedList<String>();        
    15         for (int i=0;i<L.length;i++)
    16             if (wordSet.containsKey(L[i]))
    17                 wordSet.put(L[i],wordSet.get(L[i])+1);
    18             else wordSet.put(L[i],1);
    19 
    20         int index = start;
    21         int wordLen = L[0].length();
    22         while (index+wordLen-1<S.length()){
    23             String word = S.substring(index,index+wordLen);
    24             if (!wordSet.containsKey(word)){
    25                 while(wordList.size()>0){
    26                     String temp = wordList.get(0);
    27                     wordList.remove(0);                    
    28                     wordSet.put(temp,wordSet.get(temp)+1);
    29                 }
    30                 index += wordLen;
    31              } else {
    32                 int left = wordSet.get(word);
    33                 wordSet.put(word,left-1);
    34                 wordList.add(word);                
    35                 index+=wordLen;
    36                 
    37                 if (left-1<0){
    38                     while (!wordList.get(0).equals(word)){
    39                         String temp = wordList.get(0);
    40                         wordList.remove(0);                        
    41                         wordSet.put(temp,wordSet.get(temp)+1);
    42                     }
    43                     String temp = wordList.get(0);
    44                     wordList.remove(0);                    
    45                     wordSet.put(temp,wordSet.get(temp)+1);
    46                 }
    47                 int size = wordList.size();
    48                 if (size==L.length)
    49                     res.add(index-L.length*L[0].length());
    50 
    51                
    52              }
    53          }
    54 
    55          return;
    56 
    57      }    
    58 
    59 }
  • 相关阅读:
    python,os操作文件,文件路径(上一级目录)
    python屏幕的交互(读取输出信息)input,raw_input的区别
    Shell script 传参数处理(默认变量)
    python 2.4 的字符串转时间(日期减法取间隔时间)
    java的hashcode(结合hashset讲解)
    μCOS-II系统之事件(event)的使用规则及Semaphore的相互排斥量使用方法
    HDU 1079 Calendar Game (博弈论-sg)
    flume MemoryChannel 源代码解析
    MySQL查询时强制区分大写和小写
    蓝桥杯——真题训练之李白打酒
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4122438.html
Copyright © 2011-2022 走看看