*Find All Repeating Substring With Given Length
2016-01-22 13:41
417 查看
Find all the repeating substring of specified length in a large string sequence.
For e.g.
eg.
[/code]
[/code]
However, since the length of substring is given, we can also do simply iteration: insert all substring with given length into a HashSet, and check repetition. ref
Hashset code:
reference: http://www.shuatiblog.com/blog/2015/01/11/all-repeating-substring-given-length/
For e.g.
Input String: "ABCACBABC" repeated sub-string length: 3 Output: ABC
eg.
[/code]
Input String: "ABCABCA" repeated sub-string length: 2 Output: AB, BC, CA
[/code]
Solution
Similar to [Amazon] Longest Repeating Substring, the best solution is to do Suffix Tree, or suffix array. We then need to print nodes on a certain level, who has more than 1 descendant.However, since the length of substring is given, we can also do simply iteration: insert all substring with given length into a HashSet, and check repetition. ref
Code
Suffix tree solution: not written.Hashset code:
public List<String> solve(String input, int k) { List<String> ans = new ArrayList<String>(); HashSet<String> set = new HashSet<String>(); for (int i = 0; i <= input.length() - k; i++) { String sub = input.substring(i, i + k); if (set.contains(sub)) { ans.add(sub); } set.add(sub); } return ans; }
reference: http://www.shuatiblog.com/blog/2015/01/11/all-repeating-substring-given-length/
相关文章推荐
- C函数手册
- 数据结构学习之顺序表(一)
- jquery懒加载图片的插件 jquery懒加载图片的插件
- 网络篇-NSURLSessionDownloadTask进度下载(续上节)
- 1、大部分社交平台接口不支持https协议。
- C# 常用小技巧
- 推荐!手把手教你使用Git
- Unity3D中自带事件函数的执行顺序
- Windows查看端口占用及杀掉进程
- Git配置SSH非默认端口(22)
- geoserver中除了使用kml来查询数据以外,还可以使用csql或ecsql
- unity3d生成二维码的研究
- java 实现多线程的三种基本方式
- 杭电1010——Tempter of the Bone(DFS、剪枝)
- MDK输出map文件分析
- MSChart的研究 图表
- Codeforces 441 B. Valera and Fruits
- 重新发现物理之美(二)
- C语言学习之指针高级
- ubunt12.04下的boa服务器搭建