File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ /** 대문자 문자들을 소문자로 변환하고, 알파벳이 아닌 문자들을 제거했을 때 앞뒤가 똑같이 읽히는 구문을 palindrome이라고 한다.
2
+ 주어진 구문이 palindrome인지 여부를 확인하여 boolean 값을 반환하세요.
3
+ */
4
+ class Solution {
5
+
6
+ // 투 포인터 활용 시간 복잡도: O(n), 공간복잡도: O(n)
7
+ public boolean isPalindrome (String s ) {
8
+
9
+
10
+ int left = 0 ;
11
+ int right = s .length () - 1 ;
12
+
13
+ while (left < right ) {
14
+ // 문자 또는 숫자 아닐 시
15
+ while (left < right && !Character .isLetterOrDigit (s .charAt (left ))) {
16
+ left ++;
17
+ }
18
+ while (left < right && !Character .isLetterOrDigit (s .charAt (right ))) {
19
+ right --;
20
+ }
21
+ // 양 포인터 일치하지 않을 경우 리턴
22
+ if (Character .toLowerCase (s .charAt (left )) != Character .toLowerCase (s .charAt (right ))) {
23
+ return false ;
24
+ }
25
+
26
+ left ++;
27
+ right --;
28
+
29
+ }
30
+ return true ;
31
+ }
32
+
33
+ // char 배열 활용 풀이: 시간 복잡도: O(n), 공간복잡도: O(n)
34
+ // public boolean isPalindrome(String s) {
35
+ // // non-alphanumeric (숫자 포함 - test case "0P")
36
+ // char[] convertArr = s.toLowerCase().replaceAll("[^a-z0-9]", "").toCharArray();
37
+ // int maxIdx = convertArr.length - 1;
38
+ // for (int i = 0; i <= maxIdx; i++) {
39
+ // if (convertArr[i] != convertArr[maxIdx - i]) {
40
+ // return false;
41
+ // }
42
+ // }
43
+ // return true;
44
+ // }
45
+
46
+ // StringBuffer reverse() 활용
47
+ // public boolean isPalindrome(String s) {
48
+ // // non-alphanumeric (숫자 포함 - test case "0P")
49
+ // String convertedString = s.toLowerCase().replaceAll("[^a-z0-9]", "");
50
+ // StringBuffer sb = new StringBuffer(convertedString);
51
+ // return convertedString.equals(sb.reverse().toString());
52
+ // }
53
+ }
54
+
You can’t perform that action at this time.
0 commit comments