LeetCode: Substring with Concatenation of All Words 解题报告
2014-11-22 07:32
621 查看
[b]Substring with Concatenation of All Words[/b]
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).
View Code
[b]SOLUTION 3:[/b]
九章算法官网解:
http://www.ninechapter.com/solutions/substring-with-concatenation-of-all-words/
[b]主页君GITHUB:[/b]
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/FindSubstring.java
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).
// SOLUTION 2: public List<Integer> findSubstring(String S, String[] L) { HashMap<String, Integer> map = new HashMap<String, Integer>(); HashMap<String, Integer> found; List<Integer> ret = new ArrayList<Integer>(); if (S == null || L == null || L.length == 0) { return ret; } // put all the strings into the map. for (String s: L) { if (map.containsKey(s)) { map.put(s, map.get(s) + 1); } else { map.put(s, 1); } } int lenL = L[0].length(); // 注意这里的条件:i < S.length() - lenL * L.length // 这里很关键,如果长度不够了,不需要再继续查找 for (int i = 0; i <= S.length() - lenL * L.length; i++) { // 每一次,都复制之前的hashMap. found = new HashMap<String, Integer>(map); // 一次前进一个L的length. // 注意j <= S.length() - lenL; 防止越界 for (int j = i; j <= S.length() - lenL; j += lenL) { String sub = S.substring(j, j + lenL); if (found.containsKey(sub)) { // 将找到字符串的计数器减1. found.put(sub, found.get(sub) - 1); // 减到0即可将其移出。否则会产生重复运算,以及我们用MAP为空来判断是否找到所有的单词。 if (found.get(sub) == 0) { found.remove(sub); } } else { // 不符合条件,可以break,i前进到下一个匹配位置 break; } // L中所有的字符串都已经找到了。 if (found.isEmpty()) { ret.add(i); } } } return ret; }
View Code
[b]SOLUTION 3:[/b]
九章算法官网解:
http://www.ninechapter.com/solutions/substring-with-concatenation-of-all-words/
[b]主页君GITHUB:[/b]
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/FindSubstring.java
相关文章推荐
- 【LeetCode】Substring with Concatenation of All Words 解题报告
- [LeetCode] Substring with Concatenation of All Words 解题报告
- [LeetCode] Substring with Concatenation of All Words 解题报告
- [Leetcode] 30. Substring with Concatenation of All Words 解题报告
- [leetcode] 30. Substring with Concatenation of All Words 解题报告
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
- Substring with Concatenation of All Words——解题报告(窗口移动法)
- leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
- Substring with Concatenation of All Words解题报告
- leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法
- leetcode解题方案--030--Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- [leetcode]Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode 102: Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- LeetCode: Substring with Concatenation of All Words
- leetcode_Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words