File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-palindromic-substring Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * [Problem]: [5] Longest Palindromic Substring
3+ * (https://leetcode.com/problems/longest-palindromic-substring/)
4+ */
5+ // 시간복잡도 O(n^2)
6+ // 공간복잡도 O(1)
7+ function longestPalindrome ( s : string ) : string {
8+ if ( s . length < 2 ) return s ;
9+
10+ let start = 0 ;
11+ let end = 0 ;
12+
13+ function findPalindrome ( left : number , right : number ) : void {
14+ while ( 0 <= left && right < s . length && s [ left ] === s [ right ] ) {
15+ left -- ;
16+ right ++ ;
17+ }
18+
19+ if ( right - left - 1 > end - start ) {
20+ start = left + 1 ;
21+ end = right - 1 ;
22+ }
23+ }
24+
25+ for ( let i = 0 ; i < s . length ; i ++ ) {
26+ findPalindrome ( i , i ) ;
27+ findPalindrome ( i , i + 1 ) ;
28+ }
29+
30+ return s . substring ( start , end + 1 ) ;
31+ }
You can’t perform that action at this time.
0 commit comments