File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,3 +78,5 @@ const solution = (s) => {
7878 return maxLength;
7979};
8080```
81+
82+ - 처음 코드에서는 홀수 검사 결과가 있다면 짝수 검사를 진행하지 않았었는데, 짝수 검사의 결과가 더 큰 경우도 존재함을 알게 되었다. 따라서 두 경우를 모두 계산해주고 더 큰 값을 반환해주도록 변경하였다.
Original file line number Diff line number Diff line change 1+ const isSameWord = ( word , pre , next ) => {
2+ if ( pre < 0 || next >= word . length ) {
3+ return false ;
4+ }
5+ if ( word [ pre ] !== word [ next ] ) {
6+ return false ;
7+ }
8+ return true ;
9+ } ;
10+
11+ const oddPalindrome = ( word , start ) => {
12+ let index = 1 ;
13+ while ( true ) {
14+ const pre = start - index ;
15+ const next = start + index ;
16+ if ( isSameWord ( word , pre , next ) ) {
17+ index = index + 1 ;
18+ continue ;
19+ }
20+ index = index - 1 ;
21+ break ;
22+ }
23+ return index * 2 + 1 ;
24+ } ;
25+
26+ const evenPalindrome = ( word , start ) => {
27+ let index = 0 ;
28+ while ( true ) {
29+ const pre = start - index ;
30+ const next = start + 1 + index ;
31+ if ( isSameWord ( word , pre , next ) ) {
32+ index = index + 1 ;
33+ continue ;
34+ }
35+ break ;
36+ }
37+ return index * 2 ;
38+ } ;
39+
40+ const isPalindrome = ( word , start ) => {
41+ const oddResult = oddPalindrome ( word , start ) ;
42+ const evenResult = evenPalindrome ( word , start ) ;
43+ return Math . max ( oddResult , evenResult ) ;
44+ } ;
45+
46+ const solution = ( s ) => {
47+ let maxLength = 0 ;
48+ for ( let index = 0 ; index < s . length ; index ++ ) {
49+ const result = isPalindrome ( s , index ) ;
50+ if ( maxLength < result ) {
51+ maxLength = result ;
52+ }
53+ }
54+
55+ return maxLength ;
56+ } ;
You can’t perform that action at this time.
0 commit comments