【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 ...
随机推荐
- 多线程设计模式 - Future模式之JAVA原生实现
在之前一篇博客中介绍了Future设计模式的设计思想以及具体实现,今天我们来讲一下使用JDK原生的包如何实现. JDK内置的Future主要使用到了Callable接口和FutureTask类. Ca ...
- 全文索引CONTAINS语法
Like直接在数据据中查找可以查到所有所需记录但是会扫描整个表会影响性能CONTAINS是基于全文索引进行查询,查询结果受系统全文索引分词的方法影响查询结果会不全.Select * FROM A Wh ...
- [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3669 Solved: 1451[Submit][Sta ...
- 51nod 1596 搬货物【贪心/二进制】
1596 搬货物 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 取消关注 现在有n个货物,第i个货物的重量是 2wi ...
- poj2728(最小比率生成树)
poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...
- Quaternion Euler
geometry_msgs::Quaternion orientation = map->info.origin.orientation; tf::Matrix3x3 mat(tf:: ...
- [BZOJ 1509] 逃学的小孩
Link: BZOJ 1509 传送门 Solution: 一开始受样例影响又犯了想当然的毛病……图中的C点不一定在直径上! 3次$dfs$求出树的直径及直径的两个端点$rt1,rt2$到每个点的距离 ...
- [洛谷3796]【模板】AC自动机(加强版)
题目大意: 给定$n(n\leq150)$个模式串$p_i(|p_i|\le70)$和一个$t(|t|\le10^6)$,求$t$中被匹配次数最多的$p_i$. 思路: AC自动机.匹配时记录一下匹配 ...
- Nginx的server为0.0.0.0/0.0.0.1的作用?
看到kong默认的代理和后台server 都是0.0.0.0,代理到上游的服务器proxy_pass $upstream_scheme://kong_upstream;配置如下, upstream k ...
- 服务器不安装Excel,实现导出Excel功能
/// <summary> /// 导出为Excel /// </summary> /// <param name="sender"></ ...