LeetCode 28. Implement strStr()
2016-04-24 23:48
253 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
It is pretty hard to generalize KMP algorithm under stress and limited time. Better to formalize brute force first and then move forward to KMP.
One Variation in Facebook interview.
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
It is pretty hard to generalize KMP algorithm under stress and limited time. Better to formalize brute force first and then move forward to KMP.
#include <string> #include <iostream> #include <vector> using namespace std; // brute force int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle.size(); int i = 0, j = 0; while(i < m && j < n) { if(haystack[i] == needle[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } return j == n ? i - j : -1; } // time complexity: O(m*n) // KMP to make the time complexity to be O(m + n); vector<int> next(string& needle) { vector<int> next(needle.size(), 0); for(int i = 1; i < needle.size(); ++i) { int j = next[i-1]; while(j > 0 && (needle[i] != needle[j])) j = next[j-1]; next[i] = (j + (needle[i] == needle[j])); } return next; } int strStrII(string haystack, string needle) { vector<int> nextV = next(needle); int k = 0; for(int i = 0; i < haystack.size(); ++i) { while(k > 0 && haystack[i] != needle[k]) k = nextV[k-1]; if(haystack[i] == needle[k]) k++; if(k == needle.size()) return i - needle.size() + 1; } return -1; }
One Variation in Facebook interview.
/* StrStr variation. implement function strstr(string a, string b) return index where any permutation of b is a substring of a. eg: strstr("hello", "ell") return 1 strstr("hello", "lle") return 1 strstr("hello", "wor") return -1; */ void permutate(string target, string path, set<string>& res) { if(target.size() == 0) { res.insert(path); return; } for(int i = 0; i < target.size(); ++i) { string remains = target.substr(0, i) + target.substr(i + 1); permutate(remains, path + target[i], res); } } set<string> permutate(string target) { string path = ""; set<string> res; permutate(target, path, res); return res; } int strstr(string orig, string target) { set<string> perm = permutate(target); auto iter = perm.begin(); int k = target.size(); for(int i = 0; i < orig.size() - k; ++i) { string tmp = orig.substr(i, k); if(perm.find(tmp) != perm.end()) return i; } return -1; } int main(void) { cout << strstr("hello", "ell") << endl; cout << strstr("hello", "lle") << endl; cout << strstr("hello", "wor") << endl; }
相关文章推荐
- 2_索引与查询优化
- 深入浅出的理解框架(Struts2、Hibernate、Spring)与 MVC 设计模式
- Swift3.0 / swift2.3中dispatch_group的使用
- 分布式计算——实现简单的浏览器和web服务器
- LeetCode 008
- 翁恺老师零基础学JAVA语言第五周数组--投票程序例题-个人理解
- JAVA中的File类,I/O流基础
- iOS开发系列--Objective-C之类和对象
- dup and dup2的剖析
- 编程珠玑(第一章(2):位图索引)
- 服务器之新手入门
- 虚幻4 渲染staticmesh过程。
- LeetCode 007
- leetcode刷题,总结,记录,备忘 201
- 进行iOS代码调试之LLDB调试器
- 2016腾讯de笔试online
- 20145216史婧瑶《Java程序设计》第8周学习总结
- Log打印日志遇到的问题
- [spring源码学习]二、IOC源码——配置文件读取
- 20145303《Java程序设计》实验三实验报告