File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문자열이 팰린드롬인지 확인하는 함수
3
+ * 팰린드롬 판단:
4
+ * 대문자를 소문자로 변환
5
+ * 영숫자(알파벳이랑 숫자)만 남기고 나머지 제거 => 정규식 알면 편함
6
+ * 앞에서 읽으나 뒤에서 읽으나 같아야 함
7
+ */
8
+
9
+ /**정규식 없이 문자열 뒤집는 방법
10
+ * @param {string } s
11
+ * @return {boolean }
12
+ */
13
+ var isPalindrome = function ( s ) {
14
+ s = s . toLowerCase ( ) ;
15
+
16
+ let str = '' ; // 영숫자만 남길 문자열
17
+ for ( let i = 0 ; i < s . length ; i ++ ) {
18
+ const char = s [ i ] ;
19
+ // 알파벳이거나 숫자면 str에 추가
20
+ if ( ( char >= 'a' && char <= 'z' ) || ( char >= '0' && char <= '9' ) ) {
21
+ str += char ;
22
+ }
23
+ }
24
+
25
+ // 문자열 뒤집기
26
+ let reversedStr = str . split ( '' ) . reverse ( ) . join ( '' ) ;
27
+
28
+ return str === reversedStr ;
29
+ } ;
30
+
31
+ /**정규식 사용한 투 포인터 방법
32
+ * @param {string } s
33
+ * @return {boolean }
34
+ */
35
+ var isPalindrome2 = function ( s ) {
36
+ const str = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ; // 영숫자만 남기
37
+
38
+ // 투 포인터
39
+ let left = 0 ;
40
+ let right = str . length - 1 ;
41
+
42
+ while ( left < right ) {
43
+ if ( str [ left ] !== str [ right ] ) {
44
+ return false ;
45
+ }
46
+ left ++ ;
47
+ right -- ;
48
+ }
49
+ return true ;
50
+ } ;
You can’t perform that action at this time.
0 commit comments