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
+ * 문제 설명
3
+ * - 주어진 문자열에서 가장긴 palindromic substring을 찾는 문제
4
+ *
5
+ * 아이디어
6
+ * 1) palindrom을 찾는 법(중심 확장법) + 홀수ver, 짝수ver 두 가지 경우를 모두 확인
7
+ * - two pointer 기법을 이용하여 확장하면서 가장 긴 palindromic substring을 찾는다.
8
+ */
9
+ function longestPalindrome ( s : string ) : string {
10
+ let maxLength = 0 ;
11
+ let start = 0 ;
12
+
13
+ const expand = ( l : number , r : number ) => {
14
+ while ( l >= 0 && r < s . length && s [ l ] === s [ r ] ) {
15
+ const currentLength = r - l + 1 ;
16
+ if ( currentLength > maxLength ) {
17
+ maxLength = currentLength ;
18
+ start = l ;
19
+ }
20
+ l -- ;
21
+ r ++ ;
22
+ }
23
+ } ;
24
+
25
+ for ( let i = 0 ; i < s . length ; i ++ ) {
26
+ expand ( i , i ) ;
27
+ expand ( i , i + 1 ) ;
28
+ }
29
+
30
+ return s . slice ( start , start + maxLength ) ;
31
+ }
You can’t perform that action at this time.
0 commit comments