- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontinuous-char.ts
52 lines (45 loc) · 1.09 KB
/
continuous-char.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @description 连续出现最多的字符和次数
* @author tangc1
* @date 2022-05-04 21:29:00
*/
interfaceIRes{
char: string
length: number
}
/**
* @description 双指针法
* param {string}
* return {Object}
*/
exportfunctionfindContinuousChar(str: string): IRes{
constres: IRes={
char: '',
length: 0
}
constlength=str.length
if(length===0)returnres
lettempLength=0
for(leti=0,j=0;i<length;i++){
if(str[i]===str[j]){
tempLength++
}
if(str[i]!==str[j]||i===length-1){
// 不相等,或者 i 到了末尾
if(tempLength>res.length){
res.char=str[j]
res.length=tempLength
}
tempLength=0
if(i<length-1){
j=i// 让 j 追上 i
i--
}
}
}
returnres
}
// const str1 = 'aabccceeeeee1234'
// const str2 = 'abcdef'
// console.info(findContinuousChar(str1));
// console.info(findContinuousChar(str2));