KMP Algorithm All In One
KMP Algorithm All In One
KMP 算法 All In One
// Knuth-Morris-Pratt 字符串查找算法
// Knuth-Morris-Pratt 字符串搜索算法
https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm
https://zh.wikipedia.org/wiki/KMP算法
CS 97SI
: Introduction to Programming Contests
https://web.stanford.edu/class/cs97si/
https://web.stanford.edu/class/cs97si/10-string-algorithms.pdf
ACM
https://cs.stanford.edu/group/acm/home.html
PKU JudgeOnline
LeetCode
459.
Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern
// hack:先扩大,再截取,如果还存在原字符串则说明,存在可复用的子串 ✅
let repeatedSubstringPattern = s => s.repeat(2).slice(1, -1).includes(s);
step1. why twice, double just for step 2, if twice a repeated string it still repeated too ;
step2. why slice(1, -1), in case of one chars string exist, for example: 'a' => 'aa' => ''.
function repeatedSubstringPattern(s: string): boolean {
const len = s.length;
if(len < 2) {
return false;
}
// write your code here
let temp = '';
for(let i = 0; i < (len / 2); i++) {
temp += s[i];
const reg = new RegExp(temp, 'g');
const left = s.replace(reg, '');
if(!left.length) {
return true;
}
}
return false;
// const map = new Map();
// for(let c of s) {
// if(!map.has(c)) {
// map.set(c, 1)
// } else {
// map.set(c, map.get(c) + 1)
// }
// }
// let keys = '';
// // .join('')
// for(const k of map.keys()) {
// keys += k;
// }
// // return s.replaceAll(keys, '').length > 0;
// const reg = new RegExp(keys, 'g');
// return !s.replace(reg, '').length;
}
28.
Implement strStr()
https://leetcode.com/problems/implement-strstr/
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
if(needle === "") {
return 0;
}
if(!haystack.includes(needle)) {
return -1;
}
return haystack.indexOf(needle);
};
KMP
var strStr = function (string, substring) {
// edge case: substring="" -> 0
if (substring === "") return 0;
const prefixTable = buildPrefixTable(substring);
let i = 0; // location in string
let j = 0; // location in substring
while (i < string.length && j < substring.length) {
if (string[i] === substring[j]) {
// if characters match, we can move to check next characters
i += 1;
j += 1;
} else if (j > 0) {
// when characters do not match, and we have a repeating
// suffix-prefix pair, we still need to check after the prefix
j = prefixTable[j - 1];
} else {
// if characters do no match, and no repetition, we can move on
i += 1;
}
}
return j === substring.length ? i - j : -1;
};
function buildPrefixTable(s) {
const table = [0];
let i = 1; // location in s
let j = 0; // length of repeating prefix and suffix
while (i < s.length) {
// If characters match, then the repeating prefix-suffix pair
// gets longer by 1 character
if (s[i] === s[j]) {
j += 1;
table[i] = j;
i += 1;
} else if (j > 0) {
// If the characters do no match, and we have repetition
// in suffix and prefix, we still need to check
// the character after the prefix
j = table[j - 1];
} else {
// When the characters don't match and no repeating
// suffix-prefix pair, then we can move on
table[i] = 0;
i += 1;
}
}
return table;
}
https://github.com/pheezx/Leetcode/tree/master/28-implement-strstr
https://www.youtube.com/clip/UgkxQ3QX2OGfTPrzJ_3Luus8Iv3jVIH6CkJl
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16444759.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-07-04 Flutter Charts All In One
2021-07-04 React Native & charts Al In One
2021-07-04 ucharts 获取点击数据 All In One
2020-07-04 Flutter App 真机调试 All In One
2020-07-04 BattleBots
2020-07-04 App icons generator All In One
2020-07-04 Interview of Chinese IT companies Ratings and Reviews website/app