Skip to content

Commit 8c3891e

Browse files
author
wangshan
committed
feat: 串模式匹配算法(feature)
- 字符集的方式 - 字符集配合String内置方法实现.
1 parent 71aee54 commit 8c3891e

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @Author: wangshan
3+
* @Date: 2021-11-28 16:57:18
4+
* @LastEditors: wangshan
5+
* @LastEditTime: 2021-11-28 22:59:24
6+
* @Description: 字符串匹配(模式匹配算法)
7+
*/
8+
9+
export function index(fstr: string, sstr: string, pos: number) {
10+
// debugger;
11+
let i = pos;
12+
let j = 1;
13+
let flen = fstr.length;
14+
let slen = sstr.length;
15+
16+
while (i <= flen && j <= slen) {
17+
if (fstr[i - 1] === sstr[j - 1]) {
18+
i++;
19+
j++;
20+
} else {
21+
i = i - j + 2;
22+
j = 1;
23+
}
24+
}
25+
26+
if (j > slen) {
27+
return i - slen;
28+
} else {
29+
return 0;
30+
}
31+
}
32+
33+
// 算法二,查找指定位置之后的第一个匹配的子串.
34+
/**
35+
*
36+
* @param {string} target 主串
37+
* @param {string} substr 子串
38+
* @param {number} pos 指定位置
39+
*
40+
* @returns i
41+
*/
42+
43+
export function Index(target: string, substr: string, pos: number) {
44+
// debugger;
45+
let tlen = target.length;
46+
let sublen = substr.length;
47+
let i = pos;
48+
if (pos > 0) {
49+
while (i <= tlen - sublen + 1) {
50+
// 这里循环条件的内表达式意思是,计算pos位置的范围,应当在满足字串长度时的查找范围
51+
let sub = target.substr(i - 1, sublen);
52+
53+
if (sub === substr) {
54+
return i;
55+
} else {
56+
i++;
57+
}
58+
}
59+
}
60+
61+
return -1;
62+
}

src/examples/string/01.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @Author: wangshan
3+
* @Date: 2021-11-20 23:18:00
4+
* @LastEditors: wangshan
5+
* @LastEditTime: 2021-11-28 22:49:09
6+
* @Description:串匹配算法测试-方法一
7+
*/
8+
import { index } from "../../dataStructure/string/StringSearch";
9+
10+
let tstr = "hello wolrd";
11+
12+
let needfindStr = "wo";
13+
14+
console.log(index(tstr, needfindStr, 3));

src/examples/string/02.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: wangshan
3+
* @Date: 2021-11-28 22:49:23
4+
* @LastEditors: wangshan
5+
* @LastEditTime: 2021-11-28 23:01:18
6+
* @Description: 字串模式匹配算法二
7+
*/
8+
9+
import { Index } from "../../dataStructure/string/StringSearch";
10+
11+
// 测试数据
12+
let target = "hello world, lo";
13+
14+
// console.log(Index(target, "wo", 3)); // 7
15+
// console.log(Index(target, "lo", 1)); // 4
16+
//空白串
17+
console.log(Index(target, " ", 2)); // 6
18+
// 测试查找不存在的子串
19+
console.log(Index(target, "cd", 4)); // -1
20+
21+
// 测试成功

src/examples/string/index.ts

-7
This file was deleted.

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: wangshan
33
* @Date: 2021-06-22 00:27:02
44
* @LastEditors: wangshan
5-
* @LastEditTime: 2021-11-20 21:09:27
5+
* @LastEditTime: 2021-11-28 22:51:39
66
* @Description: 入口文件
77
*/
88
// import "@/utils/index"; // 导入自定义模块
@@ -19,4 +19,9 @@
1919
// import "./examples/stack/03";
2020
// import "./examples/stack/04";
2121
// import "./examples/queue/01";
22-
import "./examples/queue/02";
22+
// import "./examples/queue/02";
23+
24+
// str
25+
26+
// import "./examples/string/01";
27+
import "./examples/string/02";

0 commit comments

Comments
 (0)