【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】
来源:互联网 发布:日本聊天软件排名 编辑:程序博客网 时间:2024/06/06 00:30
【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】
2 0
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】
- LeetCode|Substring with Concatenation of All Words(所有单词的串联的子字符串)
- LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)
- LeetCode | Substring with Concatenation of All Words(链接所有单词的子串)
- LeetCode----30. Substring with Concatenation of All Words(串联所有的words)
- Substring with Concatenation of All Words字符串中找到包含所有单词的子串
- LeetCode 30. Substring with Concatenation of All Words(所有单词的连接)
- LeetCode OJ 之 Substring with Concatenation of All Words(有所有单词组合的子串)
- Substring with Concatenation of All Words 滑动窗口方法 串联所有单词
- Substring with Concatenation of All Words 找到连接所有单词的子串(加解决找出xx子串的通用模板)
- leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法
- leetcode笔记—Substring with Concatenation of All Words加最小的窗户子串
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- 安卓高级
- C++面向对象编程(五)同名成员变量和同名函数
- 【Leetcode】 Factorial Trailing Zeroes #172
- 接口防刷,痛的领悟
- 黑马程序员——java基础——Java概述
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】
- 并查集hdu1232
- C++面向对象(六)继承的static关键字
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
- 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】
- linux线程初学之实现打字母功能
- 装备收集
- 查看Linux服务器的所占用的IP地址
- Spring Data MongoDB 二:添加、删除操作