Leetcode || Substring with Concatenation of All Words
来源:互联网 发布:ar卡片制作软件 编辑:程序博客网 时间:2024/05/16 07:25
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].
class Solution1 { /* * s: "barfoothefoobarman" * words: ["foo", "bar"] */ public List<Integer> findSubstring(String s, String[] words) { int len = words[0].length() * words.length; //6 List<Integer> result = new ArrayList<Integer>(); for(int i=0; i<s.length()-len; i++) { //s中6个位一组,遍历 StringBuffer sb = new StringBuffer(s.substring(i, i+len)); //取6个长度的字符串 for(int j=0; j<words.length; j++) { int index = sb.indexOf(words[j]); //看6个长度字符串是否包含foo或bar if(index == -1) { break; } else { sb.delete(index, index+words[0].length()); //包含了删除 } } if(sb.length() == 0) //删光了,说明不管重复与否,6个长度字符串肯定包含了["foo", "bar"] result.add(i); } return result; }}
超时,应该是需要删导致的
class Solution { /* * s: "barfoothefoobarman" * words: ["foo", "bar"] */ public List<Integer> findSubstring(String s, String[] words) { int len = words[0].length() * words.length; //6 List<Integer> result = new ArrayList<Integer>(); HashMap<String, Integer> map = new HashMap<String, Integer>(); if(s.length()<1 || words.length<1) return result; for(int i=0; i<words.length; i++) { //map得到结果:foo:1 bar:1 if(map.containsKey(words[i])) map.put(words[i], map.get(words[i])+1); else map.put(words[i], 1); } for(int i=0; i<s.length()-len; i++) { //s中6个位一组,遍历 String str = s.substring(i, i+len); //取6个长度的字符串 int from = 0; int times = 0; //进while循环的次数 while(map.containsKey(str.substring(from, from+words[0].length()))) { String word = str.substring(from, from+words[0].length()); //每3个一取 //map.put(word, map.get(word)-1); //最后其实也可以通过map来判断,如果所有单词出现的次数全为0,就对了。不过,比较麻烦 times++; from += words[0].length(); //加3 if(from >= len) break; } if(times == words.length) result.add(i); if(times > 0) { //重置map map.clear(); for(int j=0; j<words.length; j++) { if(map.containsKey(words[j])) map.put(words[j], map.get(words[j])+1); else map.put(words[j], 1); } } } return result; }}public class Mian { public static void main(String[] args) { String[] words = new String[]{"word","good","best","good"}; System.out.println(new Solution().findSubstring("wordgoodgoodgoodbestword", words)); }}
0 0
- 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
- hibernate+spring处理BLOB,CLOB字段
- 一个页面内,多个listview的实现
- android------通用ViewHolder 通用Adapter
- LeetCode---Pascal's Triangle
- 论文阅读《Spatial Transformer Networks》
- Leetcode || Substring with Concatenation of All Words
- swift UILable 文字颜色分段
- swift UIButton
- 第3条:用私有构造器或者枚举类型强化Singleton属性
- xpath相对当前节点定位方法总结
- 对于MSP430与SIM900A无法通信的问题解决
- 编程之美-求二进制数中1的个数方法整理
- zoj 1601 Count the Colorst(线段树)
- swift UIButton 设置动态图片