leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法

原创 2015年07月08日 11:11:35

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 words exactly 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).


思路:leetcode 上有些题通过率低,并不见得是算法难,我觉得很大一部分原因是题目描述不清晰,导致规则理解不透彻,所以编程的时候就会发生规则理解偏差的问题。

本题也是一样,刚一开始对规则理解偏差比较多,以为wors中字符出现在子串中出现一次即可,不管重复还是不重复,但是后面提交不过时,看case完全理解错了规则,只能重新改写代码,很麻烦。

怨言很大,规则制定和说明也是很重要的一点。

代码如下:

public class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<Integer>();
        if(words.length == 0 || s.length() == 0){
            return list;
        }
        Map<String,Integer> map = new HashMap<String,Integer>();//保存个数以及值
        for(int i = 0; i < words.length; i++){
            if(map.get(words[i]) == null){
            	map.put(words[i],1);//将word保存
            }else{
               map.put(words[i],map.get(words[i])+1);//将word保存的数值+1 
            }
        }
        Map<String,Integer> mapValue = new HashMap<String,Integer>(map);//保存重复的个数,方便重新赋值
        int len = words.length;//去除重复之后的数组长度
        
        int wordLen = words[0].length();
        String temp = "";
        int count = 0;//每个单词出现一次,就记录一次
        for(int i = 0; i <= s.length() - len*wordLen;i++){
        	count = 0;//初始化
            for(int j = 0; j < len;j++){
                temp = s.substring(i + j * wordLen,i + (j+1) * wordLen);//截取wordLen长的字符串
                if(map.get(temp) != null && map.get(temp) != 0){//如果map还有多于0个
                    map.put(temp,map.get(temp)-1);//map中数值减去1
                    count++;//记录数+1
                }else{
                    break;
                }
            }
            if(count == len){//如果记录数与len相等,则说明符合要求
                list.add(i);
            }
            //HashMap重新初始化
            for(String key:map.keySet()){//这样更快速
                map.put(key,mapValue.get(key));
            }
        }
        return list;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 30. Substring with Concatenation of All Words 解题报告

题目链接:https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a stri...
  • qq508618087
  • qq508618087
  • 2015年12月18日 04:40
  • 574

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)
  • hcbbt
  • hcbbt
  • 2015年03月06日 22:14
  • 2984

[Leetcode]30. Substring with Concatenation of All Words @python

题目You are given a string, s, and a list of words, words, that are all of the same length. Find all s...
  • qian2729
  • qian2729
  • 2016年01月16日 22:19
  • 412

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 startin...
  • a45872055555
  • a45872055555
  • 2014年08月17日 20:54
  • 466

LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)

Problem: You are given a string, s, and a list of words, words, that are all of the same length...
  • runningtortoises
  • runningtortoises
  • 2015年05月14日 13:13
  • 1122

【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 ...
  • ljiabin
  • ljiabin
  • 2014年12月29日 15:45
  • 7422

LeetCode(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 s...
  • fly_yr
  • fly_yr
  • 2015年08月24日 20:51
  • 2282

(Java)LeetCode-30. Substring with Concatenation of All Words

这道题Hard模式,比较复杂,又没有用到很经典的算法,卡在这道题很长时间了。 首先比较直观的解法package datastru; import java.util.ArrayList; imp...
  • u012848330
  • u012848330
  • 2016年07月31日 22:03
  • 299

LeetCode 30.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 al...
  • caishenfans
  • caishenfans
  • 2015年02月15日 22:58
  • 299

*[leetcode] 30.Substring with Concatenation of All Words

题目地址:https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/题目描述: You are g...
  • he1533
  • he1533
  • 2017年09月03日 11:35
  • 104
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法
举报原因:
原因补充:

(最多只允许输入30个字)