zoukankan      html  css  js  c++  java
  • [leedcode 30] Substring with Concatenation of All Words

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

    For example, given:
    s: "barfoothefoobarman"
    words: ["foo", "bar"]

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

    public class Solution {
        public List<Integer> findSubstring(String s, String[] words) {
            //本题题意是:给定一个字符串s和一个字符串数组words,words中的各字符串长度均相等。
            //找出s中所有的子串,这些子串恰好包含words中所有字符各一次,返回子串起始位置。
            //本题很难,注意题眼,words中字符串是等长的.
            //利用hashMap数据结构,key保存words中的字符串,value保存该字符串在words中出现的次数(注意重复字符串的处理)
            //遍历s时,每次substring子串的长度是固定的。定义一个计数器,当计数器等于words中的单词个数时,添加结果。
            //注意hashMap的清空以及利用其判断子串的个数的方法
            //解题步骤:
            //1.用map表示words,map中的Key为words中的各字符串,Value为该字符串出现次数
    
            //2.执行循环,用str表示S中的各字符串,cnt表示S中包含的L中字符串的个数。若map中key包含str且对应的value大于0(因为要恰好包含1次),             //则cnt//++,map中str对应的value--。若cnt==words.length,
            //则这一段满足题目要求。接着再次初始化map,继续执行循环
            //3.返回结果集
            int len=words[0].length();
            if(s==null||s.length()<1)return null;
            List<Integer> res=new ArrayList<Integer>();
            Map<String,Integer> map=new HashMap<String,Integer>();
            for(int i=0;i<words.length;i++){
                if(map.containsKey(words[i])){
                    map.put(words[i],map.get(words[i])+1);
                }else{
                    map.put(words[i],1);
                }
            }//注意i的范围
            for(int i=0;i<=s.length()-words.length*len;i++){
                int from=i;
                int con=0;
              /*  String str=s.substring(from,from+len);
                while(map.containsKey(str)&&map.get(str)>0){
                    map.put(str,map.get(str)-1);
                        con++;
                   if(con==words.length){
                        res.add(i);
                        break;
                    }
                    from+=len;
                    str=s.substring(from,from+len);
                    
                }*/
                 for(int j=0;j<words.length;j++){
                   // if((from+(j+1)*len)>s.length())break;
                    String temp=s.substring(from+j*len,from+(j+1)*len);
                    if(map.containsKey(temp)&&map.get(temp)>0){
                        map.put(temp,map.get(temp)-1);
                        con++;
                        
                    }else{
                        break;
                    }
                    
                    
                }
                if(con==words.length){
                    res.add(i);
                }
                //注意清空map
                if(con>0){
                    map.clear();
                     for(int t=0;t<words.length;t++){
                        if(map.containsKey(words[t])){
                            map.put(words[t],map.get(words[t])+1);
                        }else{
                            map.put(words[t],1);
                        }
                }
                }
                
                
            }
            return res;
        }
    }
  • 相关阅读:
    JDBC_查询
    微信web小程序开发1
    小型人口普查系统2
    小型人口普查系统1
    JSP内置对象作用域
    Session和Cookie的区别
    Cookie
    Session
    c++中嵌入python
    目标文件obj的各段 2
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4633030.html
Copyright © 2011-2022 走看看