Leetcode || Substring with Concatenation of All Words

来源:互联网 发布:ar卡片制作软件 编辑:程序博客网 时间:2024/05/16 07:25

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].

class Solution1 {    /*     * s: "barfoothefoobarman"     * words: ["foo", "bar"]     */    public List<Integer> findSubstring(String s, String[] words) {        int len = words[0].length() * words.length;  //6        List<Integer> result = new ArrayList<Integer>();        for(int i=0; i<s.length()-len; i++) {  //s中6个位一组,遍历            StringBuffer sb = new StringBuffer(s.substring(i, i+len));  //取6个长度的字符串            for(int j=0; j<words.length; j++) {                int index = sb.indexOf(words[j]);   //看6个长度字符串是否包含foo或bar                if(index == -1) {                    break;                } else {                    sb.delete(index, index+words[0].length());  //包含了删除                }            }            if(sb.length() == 0)  //删光了,说明不管重复与否,6个长度字符串肯定包含了["foo", "bar"]                result.add(i);        }        return result;    }}

超时,应该是需要删导致的

class Solution {    /*     * s: "barfoothefoobarman"     * words: ["foo", "bar"]     */    public List<Integer> findSubstring(String s, String[] words) {        int len = words[0].length() * words.length;    //6        List<Integer> result = new ArrayList<Integer>();        HashMap<String, Integer> map = new HashMap<String, Integer>();         if(s.length()<1 || words.length<1) return result;        for(int i=0; i<words.length; i++) {   //map得到结果:foo:1  bar:1            if(map.containsKey(words[i]))                map.put(words[i], map.get(words[i])+1);            else                map.put(words[i], 1);        }        for(int i=0; i<s.length()-len; i++) {  //s中6个位一组,遍历            String str = s.substring(i, i+len);    //取6个长度的字符串            int from = 0;            int times = 0; //进while循环的次数            while(map.containsKey(str.substring(from, from+words[0].length()))) {                String word = str.substring(from, from+words[0].length()); //每3个一取                //map.put(word, map.get(word)-1);                   //最后其实也可以通过map来判断,如果所有单词出现的次数全为0,就对了。不过,比较麻烦                times++;                from += words[0].length();  //加3                if(from >= len) break;            }            if(times == words.length)                result.add(i);            if(times > 0) {     //重置map                map.clear();                for(int j=0; j<words.length; j++) {                    if(map.containsKey(words[j]))                        map.put(words[j], map.get(words[j])+1);                    else                        map.put(words[j], 1);                 }            }        }        return result;    }}public class Mian {    public static void main(String[] args) {        String[] words = new String[]{"word","good","best","good"};        System.out.println(new Solution().findSubstring("wordgoodgoodgoodbestword", words));    }}
0 0
原创粉丝点击