Skip to content

Commit dfb0297

Browse files
authored
feat: add ts solution to lc problem: No.1542 (doocs#2850)
No.1542.Find Longest Awesome Substring
1 parent aa8ed86 commit dfb0297

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

solution/1500-1599/1542.Find Longest Awesome Substring/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tags:
8080

8181
而如果子字符串 $s[j,..i]$ 是“超赞字符串”,那么前缀字符串 $s[0,..i]$ 的状态 $st$ 与前缀字符串 $s[0,..j-1]$ 的状态 $st'$ 的二进制位中,最多只有一位不同。这是因为,二进制位不同,表示奇偶性不同,而奇偶性不同,就意味着子字符串 $s[j,..i]$ 中该数字出现的次数为奇数次。
8282

83-
所以,我们可以用哈希表或数组记录所有状态 $st$ 第一次出现的位置。若当前前缀字符串的状态 $st$ 在哈希表中已经存在,那么说明当前前缀字符串的状态 $st$ 与前缀字符串 $s[0,..j-1]$ 的状态 $st'$ 的二进制位中,所有位都相同,即子字符串 $s[j,..i]$ 是“超赞字符串”,更新答案的最大值。或者,我们可以枚举每一位,将当前前缀字符串的状态 $st$ 的第 $i$ 位取反,即 $st \oplus (1 << i)$,然后判断 $st \oplus (1 << i)$ 是否在哈希表中,若在,那么说明当前前缀字符串的状态 $st$ 与前缀字符串 $s[0,..j-1]$ 的状态 $st' \oplus (1 << i)$ 的二进制位中,只有第 $i$ 位不同,即子字符串 $s[j,..i]$ 是“超赞字符串”,更新答案的最大值。
83+
所以,我们可以用哈希表或数组记录所有状态 $st$ 第一次出现的位置。若当前前缀字符串的状态 $st$ 在哈希表中已经存在,那么说明当前前缀字符串的状态 $st$ 与前缀字符串 $s[0,..j-1]$ 的状态 $st'$ 的二进制位中,所有位都相同,即子字符串 $s[j,..i]$ 是“超赞字符串”,更新答案的最大值。或者,我们可以枚举每一位,将当前前缀字符串的状态 $st$ 的第 $i$ 位取反,即 $st \oplus 2^i$,然后判断 $st \oplus 2^i$ 是否在哈希表中,若在,那么说明当前前缀字符串的状态 $st$ 与前缀字符串 $s[0,..j-1]$ 的状态 $st' \oplus 2^i$ 的二进制位中,只有第 $i$ 位不同,即子字符串 $s[j,..i]$ 是“超赞字符串”,更新答案的最大值。
8484

8585
最后,返回答案即可。
8686

@@ -190,6 +190,35 @@ func longestAwesome(s string) int {
190190
}
191191
```
192192

193+
#### TypeScript
194+
195+
```ts
196+
function longestAwesome(s: string): number {
197+
const d: number[] = Array(1024).fill(-1);
198+
let [st, ans] = [0, 1];
199+
d[0] = 0;
200+
201+
for (let i = 1; i <= s.length; ++i) {
202+
const v = s.charCodeAt(i - 1) - '0'.charCodeAt(0);
203+
st ^= 1 << v;
204+
205+
if (d[st] >= 0) {
206+
ans = Math.max(ans, i - d[st]);
207+
} else {
208+
d[st] = i;
209+
}
210+
211+
for (let v = 0; v < 10; ++v) {
212+
if (d[st ^ (1 << v)] >= 0) {
213+
ans = Math.max(ans, i - d[st ^ (1 << v)]);
214+
}
215+
}
216+
}
217+
218+
return ans;
219+
}
220+
```
221+
193222
<!-- tabs:end -->
194223

195224
<!-- solution:end -->

solution/1500-1599/1542.Find Longest Awesome Substring/README_EN.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: State Compression + Prefix Sum
66+
67+
According to the problem description, the characters in the "super awesome substring" can be swapped to obtain a palindrome string. Therefore, there is at most one digit character in the "super awesome substring" that appears an odd number of times, and the rest of the digit characters appear an even number of times.
68+
69+
We can use an integer $st$ to represent the parity of the digit characters in the current prefix string, where the $i$-th bit of $st$ represents the parity of the digit character $i$, i.e., the $i$-th bit of $st$ is $1$ means that the digit character $i$ appears an odd number of times, and $0$ means that the digit character $i$ appears an even number of times.
70+
71+
If the substring $s[j,..i]$ is a "super awesome string", then the state $st$ of the prefix string $s[0,..i]$ and the state $st'$ of the prefix string $s[0,..j-1]$ differ by at most one bit in binary. This is because, if the binary bits are different, it means that the parity is different, and if the parity is different, it means that the number of times the digit appears in the substring $s[j,..i]$ is odd.
72+
73+
So, we can use a hash table or array to record the first occurrence of all states $st$. If the state $st$ of the current prefix string already exists in the hash table, it means that all bits in the binary of the state $st$ of the current prefix string and the state $st'$ of the prefix string $s[0,..j-1]$ are the same, i.e., the substring $s[j,..i]$ is a "super awesome string", and we update the maximum value of the answer. Or, we can enumerate each bit, flip the $i$-th bit of the state $st$ of the current prefix string, i.e., $st \oplus 2^i$, and then check whether $st \oplus 2^i$ is in the hash table. If it is, it means that only the $i$-th bit in the binary of the state $st$ of the current prefix string and the state $st' \oplus 2^i$ of the prefix string $s[0,..j-1]$ is different, i.e., the substring $s[j,..i]$ is a "super awesome string", and we update the maximum value of the answer.
74+
75+
Finally, return the answer.
76+
77+
The time complexity is $O(n \times C)$, and the space complexity is $O(2^C)$. Where $n$ and $C$ are the length of the string $s$ and the number of types of digit characters, respectively.
6678

6779
<!-- tabs:start -->
6880

@@ -168,6 +180,35 @@ func longestAwesome(s string) int {
168180
}
169181
```
170182

183+
#### TypeScript
184+
185+
```ts
186+
function longestAwesome(s: string): number {
187+
const d: number[] = Array(1024).fill(-1);
188+
let [st, ans] = [0, 1];
189+
d[0] = 0;
190+
191+
for (let i = 1; i <= s.length; ++i) {
192+
const v = s.charCodeAt(i - 1) - '0'.charCodeAt(0);
193+
st ^= 1 << v;
194+
195+
if (d[st] >= 0) {
196+
ans = Math.max(ans, i - d[st]);
197+
} else {
198+
d[st] = i;
199+
}
200+
201+
for (let v = 0; v < 10; ++v) {
202+
if (d[st ^ (1 << v)] >= 0) {
203+
ans = Math.max(ans, i - d[st ^ (1 << v)]);
204+
}
205+
}
206+
}
207+
208+
return ans;
209+
}
210+
```
211+
171212
<!-- tabs:end -->
172213

173214
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function longestAwesome(s: string): number {
2+
const d: number[] = Array(1024).fill(-1);
3+
let [st, ans] = [0, 1];
4+
d[0] = 0;
5+
6+
for (let i = 1; i <= s.length; ++i) {
7+
const v = s.charCodeAt(i - 1) - '0'.charCodeAt(0);
8+
st ^= 1 << v;
9+
10+
if (d[st] >= 0) {
11+
ans = Math.max(ans, i - d[st]);
12+
} else {
13+
d[st] = i;
14+
}
15+
16+
for (let v = 0; v < 10; ++v) {
17+
if (d[st ^ (1 << v)] >= 0) {
18+
ans = Math.max(ans, i - d[st ^ (1 << v)]);
19+
}
20+
}
21+
}
22+
23+
return ans;
24+
}

0 commit comments

Comments
 (0)