【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】
【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】的更多相关文章
- [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 sta ...
- [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 sta ...
- 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...
- 030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
随机推荐
- RobotFramework自动化4-批量操作案例【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/robotframework/ 前言 有时候一个页面上有多个对象需要操作,如果一个个去定 ...
- hdu 5087(次长上升子序列)
Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Mysql数据库乱码总结
今天又独到了Mysql乱码问题,过去总是匆匆的解决了就算了.这次我实在受不了了.每次都是迷迷糊糊的改好的. 这次决定好好探索一把,看了很多资料及博客之后才理解了点.先记录下来.如有不对的请指正. 1. ...
- [BZOJ1854][SCOI2010]游戏 二分图最大匹
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5316 Solved: 2128[Submit][Status] ...
- [thinkphp] 如何解析ajaxReturn返回的json字符串
success: function(data){ var dataObj=eval("("+data+")");//转换为json对象 alert(dataOb ...
- Peak
A sequence of \(n\) integers \(a_1, a_2, \dots, a_n\) is called a peak, if and only if there exists ...
- Southern African 2001 框架折叠 (拓扑序列的应用)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398377.html 题目:考虑五个图片堆叠在一起,比如下面的9 * 8 的矩阵表示的是这些图片的边缘框. 现在上面的图片 ...
- #420 Div2 C
#420 Div2 C 题意 不断把数加入到一个栈里,取数的时候要求按照 1~n 的顺序取数,每次取数保证数一定在栈里,如果要取的数不在栈头,可以选择对栈排序一次.问最少排序几次. 分析 只要栈头的数 ...
- [Git]Git 常用的操作命令
创建本地仓库 git init 获取远程仓库 git clone [url] 例如:git clone https://github.com/you/yourpro.git 创建远程仓库 添加一个新的 ...
- hdu 1500 Chopsticks DP
题目链接:HDU - 1500 In China, people use a pair of chopsticks to get food on the table, but Mr. L is a b ...