题目描述:

给定一个字符串 s 和一些长度相同的单词 words。在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

示例 1:
输入:
s = "barfoothefoobarman",
words = ["foo","bar"]
输出:
[0,9] 解释: 从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。

示例 2:
输入:
s = "wordgoodstudentgoodword",
words = ["word","student"] 输出:
[]

滑动窗口法:

    // 参考博客:
// http://www.cnblogs.com/migoo/p/9454684.html
// 用了滑动窗口的方法
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<Integer>();
// 如果s,或者是words为空,那么也返回一个空的列表
if (s.length() == 0 || s == null || words.length == 0 || words == null){
return result;
}
int size = words[0].length(), length = words.length; // 把字符串数组中的的字符串全部插入HashMap中
HashMap<String, Integer> map = generate(words);
// 窗口的不同的起点,有size个不同的起点
for (int i = 0; i < size; i++){
HashMap<String, Integer> window= new HashMap<>(); // 一个滑动的窗口
int left,right;
left = right = i;
while (right <= s.length() - size && left <= s.length() - length * size){
String word = s.substring(right, right + size);
incr(window, word);
if (!map.containsKey(word)){
window.clear();
right += size;
left = right;
continue;
}
while (window.get(word) > map.get(word)){
String w = s.substring(left, left + size);
decr(window, w);
left += size;
}
right += size;
if (right - left == size * length){
result.add(left);
}
}
}
return result;
}
private HashMap<String, Integer> generate(String[] strs){
HashMap<String, Integer> map = new HashMap<>();
for (String str : strs){
incr(map, str);
}
return map;
} private void incr(HashMap<String, Integer> map, String str) {
map.put(str, map.getOrDefault(str,0) + 1);
}
private void decr(HashMap<String, Integer> map, String str) {
Integer num = map.get(str);
if (num <= 1){
map.remove(str);
}else {
map.put(str, num - 1);
}
}

暴力法:

    // 参考博客
// https://www.nowcoder.com/discuss/87526?type=0&order=0&pos=11&page=0
// 暴力法
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<Integer>();
if (s.length() == 0 || s == null || words.length == 0 || words == null){
return result;
}
int size = words[0].length();
int length = words.length;
// 截取字符串时,取左不取右,所以这里的for循环中i的最大值可以取等号
for (int i = 0; i <= s.length() - size * length; i++){
HashMap<String, Integer> map = new HashMap<>();
for (String word : words){
map.put(word, map.getOrDefault(word, 0) + 1);
}
if (check(s,i,map,size)){
result.add(i);
}
}
return result;
} private boolean check(String s, int i, HashMap<String, Integer> map, int size) {
if (map.size() == 0){
return true;
}
if (i > s.length() || i + size > s.length()){
return false;
}
String word = s.substring(i, i + size);
if (!map.containsKey(word)){
return false;
}else {
Integer num = map.get(word);
if (num <= 1){
map.remove(word);
}else {
map.put(word, num - 1);
} return check(s, i + size, map, size);
}
}

30. 与所有单词相关联的字串、java实现的更多相关文章

  1. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  2. Leetcode——30.与所有单词相关联的字串【##】

    @author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...

  3. LeetCode(30):与所有单词相关联的字串

    Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  4. [Swift]LeetCode30. 与所有单词相关联的字串 | 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 ...

  5. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  6. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  7. LeetCode--030--串联所有单词的字串(java)

    给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要 ...

  8. Entity Framework 6 Recipes 2nd Edition(12-5)译 -> 自动删除相关联实体

    12-5. 自动删除相关联实体 问题 当一个实体被删除时,你想自动删除它相关联的实体 解决方案 假设你有一个表结构由一个course (科目), course 的classes (课程),以及enro ...

  9. 报错:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

    SqlParameter[] sp = { new SqlParameter("@nGridID",SqlDbType.BigInt), new SqlParameter(&quo ...

随机推荐

  1. Eclipse设置代码模板Code Template

    团队协作最好是使用相同的代码模板 Code Template,打开 Window -> Preference -> Java -> Code Style -> Code Tem ...

  2. Caused by: MetaException(message:Hive Schema version 2.1.0 does not match metastore's schema version 1.2.0 Metastore is not upgraded or corrupt)

    解决方案汇总: ()删除HDFS上的hive数据与hive数据库 hadoop fs -rm -r -f /tmp/hive hadoop fs -rm -r -f /user/hive ()删除My ...

  3. spring boot中ConditionalOnClass为什么没有classNotFound类加载异常

    查看原码时有很多飘红的地方, 这些import都失败的地方, 为什么在运行时没有报错? 首先这些@Configuration类没有被程序中的类引用到 其次即使引用到这个类,不一定引用到类中的具体某个方 ...

  4. @Inherited:允许子类继承父类的注解。

    在看定义注解的相关文章的时候,看到这个@Inherited注解,简单的说明并没有真正搞懂是什么意思.在网上搜索了一些相关的内容,现在把一篇文章转载过来.以便后面使用. 文章出处,转载地址:(http: ...

  5. POJ - 3461 (kmp)

    题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  6. Babelfish (关于map<string,string>的用法

    题目链接:https://vjudge.net/contest/237395#problem/A 学习博客:https://blog.csdn.net/lyy289065406/article/det ...

  7. FZU Problem 2238 Daxia & Wzc's problem

    Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题: Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d; 首先让Daxia求出数列A(0)前n项和,得 ...

  8. python3+Appium自动化03-Appium元素检测

    需要导入方法NoSuchElementException from appium import webdriver from selenium.common.exceptions import NoS ...

  9. 学习笔记:Web Storage API

    Web Storage API 提供了存储机制,通过该机制,浏览器可以安全地存储键值对,比使用 cookie 更加直观. Web Storage 包含如下两种机制: sessionStorage 为每 ...

  10. css最佳实践(reset.css)

    html, body, div, span, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,abbr, address, cite ...