Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df94fa5

Browse files
feat: add typescript solution to lc problem: No.2156
No.2156.Find Substring With Given Hash Value
1 parent 920c0c4 commit df94fa5

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
lines changed

‎solution/2100-2199/2156.Find Substring With Given Hash Value/README.md‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,46 @@
7474

7575
```
7676

77-
### **TypeScript**
78-
79-
```ts
80-
77+
### **JavaScript**
78+
79+
```js
80+
/**
81+
* @param {string} s
82+
* @param {number} power
83+
* @param {number} modulo
84+
* @param {number} k
85+
* @param {number} hashValue
86+
* @return {string}
87+
*/
88+
var subStrHash = function(s, power, modulo, k, hashValue) {
89+
power = BigInt(power);
90+
modulo = BigInt(modulo);
91+
hashValue = BigInt(hashValue);
92+
const n = s.length;
93+
let pk = 1n;
94+
let ac = 0n;
95+
// 倒序滑动窗口
96+
for (let i = n - 1; i > n - 1 - k; i--) {
97+
ac = (ac * power + getCode(s, i)) % modulo;
98+
pk = pk * power % modulo;
99+
}
100+
let ans = -1;
101+
if (ac == hashValue) {
102+
ans = n - k;
103+
}
104+
for (let i = n - 1 - k; i >= 0; i--) {
105+
let pre = getCode(s, i + k) * pk % modulo;
106+
ac = (ac * power + getCode(s, i) - pre + modulo) % modulo;
107+
if (ac == hashValue) {
108+
ans = i;
109+
}
110+
}
111+
return ans == -1 ? '' : s.substring(ans, ans + k);
112+
};
113+
114+
function getCode(str, index) {
115+
return BigInt(str.charCodeAt(index) - 'a'.charCodeAt(0) + 1);
116+
}
81117
```
82118

83119
### **...**

‎solution/2100-2199/2156.Find Substring With Given Hash Value/README_EN.md‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,46 @@ Note that "bxz" also has a hash of 32 but it appears later than "
6666

6767
```
6868

69-
### **TypeScript**
70-
71-
```ts
72-
69+
### **JavaScript**
70+
71+
```js
72+
/**
73+
* @param {string} s
74+
* @param {number} power
75+
* @param {number} modulo
76+
* @param {number} k
77+
* @param {number} hashValue
78+
* @return {string}
79+
*/
80+
var subStrHash = function(s, power, modulo, k, hashValue) {
81+
power = BigInt(power);
82+
modulo = BigInt(modulo);
83+
hashValue = BigInt(hashValue);
84+
const n = s.length;
85+
let pk = 1n;
86+
let ac = 0n;
87+
// 倒序滑动窗口
88+
for (let i = n - 1; i > n - 1 - k; i--) {
89+
ac = (ac * power + getCode(s, i)) % modulo;
90+
pk = pk * power % modulo;
91+
}
92+
let ans = -1;
93+
if (ac == hashValue) {
94+
ans = n - k;
95+
}
96+
for (let i = n - 1 - k; i >= 0; i--) {
97+
let pre = getCode(s, i + k) * pk % modulo;
98+
ac = (ac * power + getCode(s, i) - pre + modulo) % modulo;
99+
if (ac == hashValue) {
100+
ans = i;
101+
}
102+
}
103+
return ans == -1 ? '' : s.substring(ans, ans + k);
104+
};
105+
106+
function getCode(str, index) {
107+
return BigInt(str.charCodeAt(index) - 'a'.charCodeAt(0) + 1);
108+
}
73109
```
74110

75111
### **...**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} power
4+
* @param {number} modulo
5+
* @param {number} k
6+
* @param {number} hashValue
7+
* @return {string}
8+
*/
9+
var subStrHash = function(s, power, modulo, k, hashValue) {
10+
power = BigInt(power);
11+
modulo = BigInt(modulo);
12+
hashValue = BigInt(hashValue);
13+
const n = s.length;
14+
let pk = 1n;
15+
let ac = 0n;
16+
// 倒序滑动窗口
17+
for (let i = n - 1; i > n - 1 - k; i--) {
18+
ac = (ac * power + getCode(s, i)) % modulo;
19+
pk = pk * power % modulo;
20+
}
21+
let ans = -1;
22+
if (ac == hashValue) {
23+
ans = n - k;
24+
}
25+
for (let i = n - 1 - k; i >= 0; i--) {
26+
let pre = getCode(s, i + k) * pk % modulo;
27+
ac = (ac * power + getCode(s, i) - pre + modulo) % modulo;
28+
if (ac == hashValue) {
29+
ans = i;
30+
}
31+
}
32+
return ans == -1 ? '' : s.substring(ans, ans + k);
33+
};
34+
35+
function getCode(str, index) {
36+
return BigInt(str.charCodeAt(index) - 'a'.charCodeAt(0) + 1);
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /