【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 ...
随机推荐
- ./configure时候遇到的问题 Cannot find install-sh, install.sh, or shtool in ac-aux
https://blog.csdn.net/anloan/article/details/17268997
- Ajax不能跨域访问的解决方案
文章介绍 这是一篇,引导文吧... 因为写这篇文章时,实在想不出该如何分序.因此以实现跨域访问为目的,从基础知识往上写.最后以百度搜索智能提示为例,来讲解跨域的具体应用! 内容 首先,我们得明确什 ...
- 51nod 1101 换零钱 【完全背包变形/无限件可取】
1101 换零钱 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...
- TaobaoVM
作者:Andoter链接:https://www.zhihu.com/question/275665265/answer/416021488来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- Xcode_9_beta.xip 更新下载
Xcode_9_beta.xip 更新下载 Xcode_9_beta.xip 链接: pan.baidu.com/s/1dFJ33tJ 密码: 89mv
- 八. 输入输出(IO)操作2.面向字符的输入流
字符流是针对字符数据的特点进行过优化的,因而提供一些面向字符的有用特性,字符流的源或目标通常是文本文件. Reader和Writer是java.io包中所有字符流的父类.由于它们都是抽象类,所以应使用 ...
- MailKit---如何知道文件夹下有多少封未读邮件
如果在mailkit中,文件夹已经选中并打开了的话,那直接使用ImapFolder.Unread属性就可以获取到有多少封未读邮件了. 如果文件夹没有打开,那么你还可以使用查询状态的方法来获取未读状态的 ...
- 能上架App的GooglePlay开发者账号获取流程
googleplay 开发者账号申请流程 接到公司号召,要让我们的app走向世界,上架GooglePlay,都说天朝的Android 程序员是折翼的天使,猛然发现写了做么多年的Android,竟然不知 ...
- 关于iframe的高度自适应问题(js)
function SetCwinHeight() { var cwin=document.getElementById("cwin"); if (document.getEleme ...
- linux 关闭ping探测
先切换到root echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 1代表关闭 0代表开启 用iptables iptables -I INPUT ...