File tree 1 file changed +38
-0
lines changed
longest-palindromic-substring
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * TC: O(N^2)
3
+ * 주어진 s 문자열이 한 종류의 문자로 이루어져있다면 for문에서 O(N), while문에서 O(N) 이므로 O(N * 2N)
4
+ *
5
+ * SC: O(1)
6
+ */
7
+
8
+ /**
9
+ * @param {string } s
10
+ * @return {string }
11
+ */
12
+ var longestPalindrome = function ( s ) {
13
+ let result = "" ;
14
+
15
+ for ( let index = 0 ; index < s . length ; index ++ ) {
16
+ const [ start1 , end1 ] = getPalindromicSubstringLength ( index , index ) ;
17
+ const [ start2 , end2 ] = getPalindromicSubstringLength ( index , index + 1 ) ;
18
+
19
+ if ( result . length < end1 - start1 + 1 ) {
20
+ result = s . substring ( start1 , end1 + 1 ) ;
21
+ }
22
+
23
+ if ( result . length < end2 - start2 + 1 ) {
24
+ result = s . substring ( start2 , end2 + 1 ) ;
25
+ }
26
+ }
27
+
28
+ return result ;
29
+
30
+ function getPalindromicSubstringLength ( start , end ) {
31
+ while ( 0 <= start && end < s . length && s [ start ] === s [ end ] ) {
32
+ start -= 1 ;
33
+ end += 1 ;
34
+ }
35
+
36
+ return [ start + 1 , end - 1 ] ;
37
+ }
38
+ } ;
You can’t perform that action at this time.
0 commit comments