Hard-题目44:30. Substring with Concatenation of All Words

转载 2016年05月31日 23:51:15

题目原文:
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).
题目大意:
给出一个字符串s和一个单词列表words,其中它们的长度都相同。求出所有子串的起始点坐标,使得这个子串是words列表中单词的“无缝连接”。
例如
s: “barfoothefoobarman”
words: [“foo”, “bar”]
则答案为[0,9]
因为s从0开始的子串为”barfoo”,从9开始的子串为”foobar”
题目分析:
来自discuss里面一位大神写的代码,有时间慢慢看吧。。。。。。。。。。。。。。
源码:(language:java)

public class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        int N = s.length();
        List<Integer> indexes = new ArrayList<Integer>(s.length());
        if (words.length == 0) {
            return indexes;
        }
        int M = words[0].length();
        if (N < M * words.length) {
            return indexes;
        }
        int last = N - M + 1;

        //map each string in words array to some index and compute target counters
        Map<String, Integer> mapping = new HashMap<String, Integer>(words.length);
        int [][] table = new int[2][words.length];
        int failures = 0, index = 0;
        for (int i = 0; i < words.length; ++i) {
            Integer mapped = mapping.get(words[i]);
            if (mapped == null) {
                ++failures;
                mapping.put(words[i], index);
                mapped = index++;
            }
            ++table[0][mapped];
        }

        //find all occurrences at string S and map them to their current integer, -1 means no such string is in words array
        int [] smapping = new int[last];
        for (int i = 0; i < last; ++i) {
            String section = s.substring(i, i + M);
            Integer mapped = mapping.get(section);
            if (mapped == null) {
                smapping[i] = -1;
            } else {
                smapping[i] = mapped;
            }
        }

        //fix the number of linear scans
        for (int i = 0; i < M; ++i) {
            //reset scan variables
            int currentFailures = failures; //number of current mismatches
            int left = i, right = i;
            Arrays.fill(table[1], 0);
            //here, simple solve the minimum-window-substring problem
            while (right < last) {
                while (currentFailures > 0 && right < last) {
                    int target = smapping[right];
                    if (target != -1 && ++table[1][target] == table[0][target]) {
                        --currentFailures;
                    }
                    right += M;
                }
                while (currentFailures == 0 && left < right) {
                    int target = smapping[left];
                    if (target != -1 && --table[1][target] == table[0][target] - 1) {
                        int length = right - left;
                        //instead of checking every window, we know exactly the length we want
                        if ((length / M) ==  words.length) {
                            indexes.add(left);
                        }
                        ++currentFailures;
                    }
                    left += M;
                }
            }

        }
        return indexes;
    }
}

成绩:
12ms,96.98%,20ms,9.38%

相关文章推荐

[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
  • 2818

30. Substring with Concatenation of All Words (又超时了,最后一个case没过)

You are given a string, s, and a list of words, words, that are all of the same length. Find all sta...

[LeetCode] 30. Substring with Concatenation of All Words

leetcode string

Leetcode||30.Substring with Concatenation of All Words

30. Substring with Concatenation of All Words Total Accepted: 68319Total Submi...

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

LeetCode 30 - Substring with Concatenation of All Words

一、问题描述 Description:Description: You are given a string, ss, and a list of words, wordswords, ...

LeetCode 30 Substring with Concatenation of All Words(AC自动机+滑动窗口)

题意:给出一个字符串,和一个字典,字典中有若干个单词(可能有重复单词),每个单词的长度相等,现在要求所有字典中全部单词的任意组合在字符串中的起始下标。 思路:首先先说一下用ac自动机复杂度可以优化到O...

【一天一道LeetCode】#30. Substring with Concatenation of All Words

注:这道题之前跳过了,现在补回来一天一道LeetCode系列(一)题目 You are given a string, s, and a list of words, words, that ar...

[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 all ...

LeetCode 30: Substring with Concatenation of All Words

Substring with Concatenation of All WordsYou are given a string, s, and a list of words, words, that...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目44:30. Substring with Concatenation of All Words
举报原因:
原因补充:

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