leetcode 71: Substring with Concatenation of All Words
来源:互联网 发布:mac系统绘画软件 编辑:程序博客网 时间:2024/05/24 03:19
Substring with Concatenation of All WordsFeb 24 '12
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).
public class Solution { public ArrayList<Integer> findSubstring(String S, String[] L) { // Start typing your Java solution below // DO NOT write main() function ArrayList<Integer> res = new ArrayList<Integer>(); if(L.length<1 || S.length() < L.length*L[0].length() ) return res; Map<String, Integer> map = new HashMap<String,Integer>(); for(int i=0; i<L.length;i++) { if( map.containsKey( L[i] ) ) { map.put( L[i], map.get(L[i])+1) ; } else { map.put(L[i], 1); } } int i=0; int Slen = S.length(); int Llen = L.length; int sz = L[0].length(); while( Slen-i >= Llen*sz){ Map<String, Integer> temp = new HashMap<String,Integer>(map); for(int j=0; j<Llen; j++) { String sub = S.substring(i+j*sz, i+(j+1)*sz); if( temp.containsKey(sub) ) { int x = temp.get(sub); if( x>1) temp.put(sub, temp.get(sub)-1); else if( x==1) temp.remove( sub); }else break; } if(temp.isEmpty() ) res.add(i); ++i; } return res; }}
- 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-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 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
- Substring with Concatenation of All Words -- LeetCode
- Leetcode: Substring with Concatenation of All Words
- leetcode:Substring with Concatenation of All Words
- LeetCode|Substring with Concatenation of All Words
- cocos2d-x游戏实例 《简单棋》入门尝试(五) 玩家控制效果的实现
- how to install xenomai(and some comments)
- 【D3D11游戏编程】学习笔记二十一:Cube Mapping及其应用之一:天空盒的实现
- Secret of the JavaScript Ninja 学习笔记 - 1
- huffman编码及解码实现
- leetcode 71: Substring with Concatenation of All Words
- 微软认证考试70-461 Work with Data 数据处理 --27%比重--(5.5)
- ehcahce 的页面缓存 配置简单
- LeetCode3:Longest Substring Without Repeating Characters
- java 字符串 转数组 Java – How to convert String into Array
- C#中ref和out的使用小结
- ZOJ 2099 Frame PolygonalLine
- 谷歌如何通过安卓赚钱
- LeetCode4: Add Two Numbers