【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

【030-Substring with Concatenation of All Words(串联全部单词的子串)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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,wrods中的字符串长度都相等。找出s中全部的子串恰好包括words中全部字符各一次,返回子串的起始位置。

解题思路

  把words转化为一个HashMap

代码实现

算法实现类

import java.util.*;

public class Solution {

    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<Integer>();
        if (words.length == 0) return list;
        int wLen = words[0].length();
        int len = s.length();
        if (len < wLen * words.length) return list;
        Map<String, Integer> mapW = new HashMap<String, Integer>();
        for (String word : words)
            mapW.put(word, mapW.containsKey(word) ? mapW.get(word) + 1 : 1);
        for (int start = 0; start < wLen; start++) {
            int pos = start;
            int tStart = -1;
            Map<String, Integer> mapT = new HashMap<String, Integer>(mapW);
            while (pos + wLen <= len) {
                String cand = s.substring(pos, pos + wLen);
                if (!mapW.containsKey(cand)) {
                    if (tStart != -1) mapT = new HashMap<String, Integer>(mapW);
                    tStart = -1;
                } else if (mapT.containsKey(cand)) {
                    tStart = tStart == -1 ?

pos : tStart;
                    if (mapT.get(cand) == 1) mapT.remove(cand);
                    else mapT.put(cand, mapT.get(cand) - 1);
                    if (mapT.isEmpty()) list.add(tStart);
                } else {
                    while (tStart < pos) {
                        String rCand = s.substring(tStart, tStart + wLen);
                        if (cand.equals(rCand)) {
                            tStart += wLen;
                            if (mapT.isEmpty()) list.add(tStart);
                            break;
                        }
                        tStart += wLen;
                        mapT.put(rCand, mapT.containsKey(rCand) ? mapT.get(rCand) + 1 : 1);
                    }
                }
                pos += wLen;
            }
        }
        return list;
    }
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47064933

时间: 2024-12-10 02:17:49

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】的相关文章

[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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input

[LeetCode] 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, giv

【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return

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

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 030. Substring with Concatenation of All Words (Hard) 链接: 题目:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 代码(github):https://gi

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【014-Longest Common Prefix(最长公共前缀)】

[014-Longest Common Prefix(最长公共前缀)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Write a function to find the longest common prefix string amongst an array of strings. 题目大意 写一个函数找出一个字串所数组中的最长的公共前缀. 解题思路 第一步先找出长度最小的字符串,然后将这个字符串与其它的字符串相比找出最短的最公共前缀. 代码实现 publi

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】

[030-Substring with Concatenation of All Words(串联所有单词的子串)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 concaten

【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid paren

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example