新博文地址:[leetcode]Substring with Concatenation of All Words
Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
这道题,坑爹!!!时间复杂度已经不能再优化了,居然还是过不了,提交了N次啊,N次啊!!!!最后听同学的建议,对细节上进行了优化,其实就是做了一个剪枝(只是减少了常数次检查啊,居然就过了,oj要不要卡的这么紧,哭)
思路是这样的:
从第一个字符完后遍历,每次截取wordLength个长度的子串,看是否是字典中的单词,如不是,startIndice++,继续,如是(记录这个起点下标startIndice),就继续截取下面的wordLength长度。直到->
1. 把字典遍历完了,good,将startIndice加入list中
2. 字典没有遍历完,但是遇到这个子串不是字典单词,则返回,startIndice ++ ,从新开始遍历
代码如下:
public List<Integer> findSubstring(String S, String[] L) {
List<Integer> list = new ArrayList<Integer>();
if(L == null || L.length == 0 || S.length() < L[0].length()){
return list;
}
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(int i = 0 ; i < L.length; i++){
map.put(L[i], map.get(L[i]) == null ? 1 : map.get(L[i]) + 1);
}
int wordLength = L[0].length();
boolean firstMatch = true;
Map<String,Integer> copyMap = ( HashMap<String,Integer> )map.clone();
for(int startIndice = 0 ,j = 0 ; startIndice <= S.length() - wordLength * L.length && j <= S.length() - wordLength;){
String str = S.substring(j, wordLength + j);
if(copyMap.containsKey(str) && copyMap.get(str) > 0){
if(firstMatch){//第一个单词匹配成功,记录起点下标
startIndice = j;
firstMatch = false;
}
copyMap.put(str, copyMap.get(str) - 1);
j += wordLength;
if(j - startIndice == wordLength * L.length){//如果长度达到了字典的总长度,则匹配成功
list.add(startIndice);
firstMatch = true;
j = startIndice + 1;
copyMap = ( HashMap<String,Integer> )map.clone();//更新字典,以便重新匹配
}
}else{
if(!firstMatch){
j = startIndice + 1;//如果之前匹配了几个单词,但是后面的不匹配,从七点下标后一位开始遍历
firstMatch = true;
copyMap = ( HashMap<String,Integer> )map.clone();
}else{//如果不匹配,遍历下一个字母
j++;
}
startIndice++;
}
}
return list;
}
图中红色标记的是后来修改的剪枝条件,其实对改不改对时间复杂度都没有影响,但是在细节的时间上的确是缩短了一点,就这么一点,卡了我一整天.....还是对细节考虑不周啊,以此为戒吧
相关推荐
本文详细解析了LeetCode上编号为30的算法题目“Substring with Concatenation of All Words”,以及如何使用JavaScript来实现其解决方案。题目要求编写一个函数,接受两个字符串s和words作为参数,s是主字符串,...
c c语言_leetcode 0030_substring_with_concatenation_of_all_words.zip
本题解针对LeetCode上的“Number of Valid Words for Each Puzzle”题目,需要用户熟悉LeetCode的使用方法和题库。 3. 字符串处理:在解决此题目时,涉及到对字符串的操作,如判断字符串中的字符集、字符串比较、...
leetcode 跳跃 LeetCode Exercises from leetcode. All the exercises is uesd by microsoft to interview. The following is the number of the questions form Leetcode. 4 寻找两个正序数组的中位数 median of ...
Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 Group Anagrams 排序 unordered_map Minimum Window Substring 两个指针遍历 map Maximal ...
【Golang】leetcode-golang,LeetCode solutions with Golang(Golang 实现) (Leetcode golang, LeetCode solutions with Golang (Golang implementation)) 【Golang】leetcode-golang,LeetCode solutions with Golang...
第一部分的笔记《LeetCode 刷题笔记 with Java 1-50》主要包含了LeetCode前50题的解法,包括基础的数组操作、字符串处理、链表操作等。例如,"Two Sum"(两数之和)题目中,开发者会学习到如何高效地查找两个数字的...
java java_leetcode题解之Product of Array Exclude Itself.java
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...
Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) repository. I'll keep updating for full summary and...
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
leetcode-with-python3 ARRAY 1.两数之和 4.寻找两个有序数组的中位数 11.盛最多水的容器 26.删除排序数组中的重复项 15.三数之和 16.最接近的三数之和 27.移除元素 35.搜索插入位置 66.加一 88.合并两个有序数组
算法 Leetcode刷题手册 labuladong的算法小抄官方完整版
with leetcode Practice List String 28 14 58 387 383 344 151 186 345 205 293 294 290 242 49 249 87 161 38 358 316 271 168 171 13 12 273 246 247 提高 157 158 68 65 Substring 76 30 3 340 395 159 ...
With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - ...
Leetcode-with-python python solution for leetcode 数组 [283] 移动零 双指针 [345] 反转字符串中的元音字母 图 [785] 判断二分图 [207] 课程表 [886] 可能的二分法 [133] 克隆图 [684] 冗余连接 动态规划 [62] ...
周赛难度每周-Leetcode-with-MS 业主:兰 时间:从 19.12.11 Week6 动态规划 指数 标题 解决方案 验收 困难 70 √ 45.6% 简单的 198 √ 41.5% 简单的 303 √ 40.8% 简单的 64 √ 49.7% 中等的 309 √ 45.2% 中等的 ...
Reverse words in a string-leetcode
leetcode-with-js Solving LeetCode problems using JavaScript。 题目涵盖了 LeetCode、「程序员的面试金典」(Cracking the Coding Interview)[第六版] 以及「剑指 offer」。 I. 项目说明 项目结构 1. src src ...
Solution to LeetCode written by C++. All codes are tested to ACCEPTED on LeetCode online judge. Basic data structure and algorithm sample codes.