3
3
* @return {string }
4
4
*/
5
5
const longestPalindrome = ( s ) => {
6
- original_s = s ;
6
+ if ( 1 === s . length ) return s ;
7
+
7
8
s = reformat ( s ) ;
8
9
9
- let l = Array ( s . length ) . fill ( 0 ) , highest_length = 0 , maxRight = 0 , center = 0 ;
10
+ let tmp_count = 0 , highest_length = 0 , maxRight = 0 , center = 0 ;
10
11
11
- console . log ( `s: ${ s } , l: ${ l } , h: ${ highest_length } , center: ${ center } , right: ${ maxRight } , sl: ${ s . length } ` ) ;
12
+ console . log ( `s: ${ s } , h: ${ highest_length } , center: ${ center } , right: ${ maxRight } , sl: ${ s . length } ` ) ;
12
13
13
14
for ( let i = 0 ; i < s . length ; i ++ ) {
14
15
// left = (i * 2) - i;
15
16
// console.log("___ : " + s[left] + ", i: " + i + ", left: " + left)
17
+ console . log ( "_________________________________" )
16
18
console . log ( "----> i: " + i + ", s[i]: " + s [ i ] )
17
- // left
19
+
20
+ let new_i = i ;
18
21
for ( let j = 0 ; j < i ; j ++ ) {
19
22
console . log ( "---> i: " + i + ", j: " + j )
20
23
left = s [ i - ( j + 1 ) ] ;
21
24
right = s [ i + ( j + 1 ) ] ;
22
25
console . log ( "left: (" + ( i - ( j + 1 ) ) + ") - " + left )
23
26
console . log ( "right: (" + ( i + ( j + 1 ) ) + ") - " + right )
24
27
28
+ // if (i < maxRight) ...
29
+
25
30
if ( left !== right ) {
26
- console . log ( "break bc the first neighbour already unmatched" )
31
+ console . log ( "break when the neighbour is unmatched" )
27
32
break ;
28
33
}
29
34
30
35
console . log ( "ctn ..." )
31
- // console.log(l[i])
32
36
33
- l [ i ] = l [ i ] + 1 ;
34
- if ( l [ i ] > highest_length ) {
35
- highest_length = l [ i ] ;
37
+ tmp_count ++ ;
38
+
39
+ if ( tmp_count > highest_length ) {
40
+ console . log ( "--------> maybe highest: " + tmp_count )
41
+ highest_length = tmp_count ;
36
42
center = i ;
43
+ maxRight = i + tmp_count ;
44
+ console . log ( "--------> maxRight: " + maxRight )
45
+ // new_i = i + (tmp_count - 1);
46
+ // console.log("new_i: " + i)
37
47
}
38
-
39
- console . log ( l [ i ] )
40
48
}
49
+ // i = new_i;
50
+ // console.log("i += tmp_count: " + (i));
41
51
52
+ tmp_count = 0 ;
42
53
}
43
54
44
- console . log ( "finally list of match count: " + l )
45
55
console . log ( "highest_length: " + highest_length )
46
56
console . log ( "center: " + center )
47
57
s_part = s . substring ( ( center - highest_length ) , ( center + highest_length ) ) ;
58
+ console . log ( "s_part: " + s_part )
48
59
answer = s_part . replace ( / # / g, "" ) ;
49
60
console . log ( answer ) ;
50
61
return answer ;
@@ -62,4 +73,6 @@ const reformat = (s) => {
62
73
}
63
74
64
75
longestPalindrome ( "NBNNBR" )
76
+ // longestPalindrome("cbbd")
77
+ // longestPalindrome("ccc")
65
78
// longestPalindrome("qweeeewqwe")
0 commit comments