File tree Expand file tree Collapse file tree
src/kyu9341/programmers_가장긴팰린드롬 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const getLongestPalindrome = ( left , right , arrFromStr ) => {
2+ let maxLength = 0 ;
3+ const isInRange = ( left , right ) => left >= 0 && right < arrFromStr . length ;
4+
5+ while ( isInRange ( left , right ) ) {
6+ if ( arrFromStr [ left ] !== arrFromStr [ right ] ) return maxLength ;
7+
8+ maxLength = right - left + 1 ;
9+ left -= 1 ;
10+ right += 1 ;
11+ }
12+
13+ return maxLength ;
14+ } ;
15+
16+ const solution = s => {
17+ let maxLength = 0 ;
18+ const arrFromStr = [ ...s ] ;
19+
20+ const checkLength = ( _ , index ) => {
21+ const odd = getLongestPalindrome ( index , index , arrFromStr ) ;
22+ const even = getLongestPalindrome ( index , index + 1 , arrFromStr ) ;
23+ maxLength = Math . max ( maxLength , Math . max ( odd , even ) ) ;
24+ } ;
25+
26+ arrFromStr . forEach ( checkLength ) ;
27+
28+ return maxLength ;
29+ } ;
30+
31+ ( ( ) => {
32+ const inputs = [ 'abcdcba' , 'abacde' ] ;
33+
34+ inputs . forEach ( input => console . log ( solution ( input ) ) ) ;
35+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments