String——substring-with-concatenation-of-all-words(模式匹配变型)
来源:互联网 发布:北京幼儿园 知乎 编辑:程序博客网 时间:2024/06/03 04:09
字符串的模式匹配变型
1)L中存在重复的元素
2)子串不允许间断,即子串从开始到找全L中的所有元素之前,子串中不允许包含L以外的东西,
而且,即使当前处理的子串是L中含有的,但是前面已经找够了,这个多余的也是不合法的,
若此时还有L中的其他元素没找到,从这个起点开始也是不成功的。
3)L在S中出现的顺序不同考虑,任意顺序,只要全部存在就可以。
分析:
1)先对L中的所有元素做个统计,定义一个hash map<string, int> 型 变量total,统计每个词出现的次数,
另外定义一个同类型的count,用来记录到目前为止,已经找到的L中的元素情况,
当全部找全的时候就找到了一个合法的起始点。
然后将count清空,继续找。
2)整体的框架与传统的字符串匹配一致,不同的是这里不要求顺序,所以似乎在S中不能加速移动。
//字符串的模式匹配变型import java.util.*;public class Solution { public ArrayList<Integer> findSubstring(String s, String[] strs) { ArrayList<Integer> array=new ArrayList(); if(s == null||s.length() == 0||strs == null||strs.length == 0||s.length() < strs[0].length()){ return array; } HashMap<String,Integer> map=new HashMap(); for(int i=0;i<strs.length;i++){//统计每个串出现的次数 if(!map.containsKey(strs[i])){ map.put(strs[i],1); }else{ map.put(strs[i],map.get(strs[i])+1); } } HashMap<String,Integer> count;//暂时统计出现次数 int inlen=strs[0].length(); int dstlen=inlen*strs.length; for(int i=0;i <= s.length()-dstlen;i++){ int j=i; count=new HashMap(); int cur=0; for(;cur<strs.length;cur++){ String curstr=s.substring(j,j+inlen); if(!map.containsKey(curstr)){ break; } if(!count.containsKey(curstr)){ count.put(curstr,1); }else if(count.get(curstr) < map.get(curstr)){ count.put(curstr,count.get(curstr)+1); }else{ break; } j=j+inlen; } if(cur == strs.length){ array.add(i); } } return array; }}
0 0
- String——substring-with-concatenation-of-all-words(模式匹配变型)
- 【leetcode】String——Substring with Concatenation of All Words(30)
- leetcode 030 —— Substring with Concatenation of All Words
- LeetCode题解——Substring with Concatenation of All Words
- Leetcode——30. Substring with Concatenation of All Words
- LeetCode——Substring with Concatenation of All Words
- <30>——Substring with Concatenation of All Words
- Substring with Concatenation of All Words——解题报告(窗口移动法)
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- LeetCode29: Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- Android数据存储之SharedPreferences详解
- linux 设置 环境变量
- ios应用数据存储方式(偏好设置)
- 第十周项目3——警察和厨师1
- 283. Move Zeroes [easy] (Python)
- String——substring-with-concatenation-of-all-words(模式匹配变型)
- sql 去重
- double 如何转为 long
- Java面试题--内存
- springMVC @ModelAttribute学习
- PAT甲级 A1025.PAT RANKING
- 自制水晶按钮
- 7.34Unix卸载oracle
- OK6410 SDIO WIFI开机自启做热点(局域网)