Skip to content

Commit 56d86c3

Browse files
committed
feat: valid-palindrome solve
1 parent b1ba303 commit 56d86c3

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

valid-palindrome/haxr369.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* 1번 풀이가 2번 풀이를 개선한 풀이
3+
* 2번 풀이는 전체 조회를 2번 하지만, 1번에서는 s를 한번만 조회하고 바로 비교할 수 있게 개선.
4+
*/
5+
class Solution {
6+
7+
/**
8+
* 문자열 s를 한번만 조회하면서 알파벳-숫자가 아닌 문자는 스킵하고
9+
* 대문자는 소문자로 변환 후 왼쪽 문자와 오른쪽 문자를 비교
10+
*
11+
* Runtime: 1 ms (Beats 100.00%)
12+
* Memory: 44.24 MB (Beats 53.06%)
13+
* Space Complexity: O(1)
14+
* - 문자열 s를 순회하면서 왼쪽 오른쪽 문자, 아스키 숫자를 저장
15+
* > O(1)
16+
* Time Complexity: O(N)
17+
* - 문자열 s를 전체 조회 => O(N)
18+
* > O(N) => O(N)
19+
*/
20+
public boolean isPalindrome(String s) {
21+
22+
// System.out.println(buffer);
23+
int leftIdx = 0;
24+
int rightIdx = s.length() - 1;
25+
while (leftIdx < rightIdx) {
26+
char leftC = s.charAt(leftIdx);
27+
char rightC = s.charAt(rightIdx);
28+
int leftNum = (int) leftC;
29+
int rightNum = (int) rightC;
30+
31+
// 대문자면 소문자로 변환
32+
if (65 <= leftNum && leftNum <= 90) {
33+
leftC = (char) (leftNum + 32);
34+
} else if (!(97 <= leftNum && leftNum <= 122) && !(48 <= leftNum && leftNum <= 57)) {
35+
// 알파벳, 숫자가 아닌 문자인 경우 스킵!
36+
leftIdx++;
37+
continue;
38+
}
39+
40+
if (65 <= rightNum && rightNum <= 90) {
41+
rightC = (char) (rightNum + 32);
42+
} else if (!(97 <= rightNum && rightNum <= 122) && !(48 <= rightNum && rightNum <= 57)) {
43+
rightIdx--;
44+
continue;
45+
}
46+
47+
if (leftC != rightC) {
48+
return false;
49+
}
50+
// 두 문자가 동일하면 다음 문자를 체크하기
51+
leftIdx++;
52+
rightIdx--;
53+
}
54+
55+
return true;
56+
}
57+
58+
/**
59+
* 모든 대문자를 소문자로 만들고
60+
* 알파벳이 아닌 문자를 다 제거한 문장이, 대칭이면 true이다. 아님 false.
61+
*
62+
* 1. 문장을 스캔하면서 대문자는 소문자로 buffer에 넣고, 영어가 아닌 문자는 넣지 않기
63+
* 2. 판단할 때 아스키 코드를 이용하기
64+
*
65+
* Runtime: 121 ms (Beats 7.53%)
66+
* Memory: 48.00 MB (Beats 5.23%)
67+
* Space Complexity: O(1)
68+
* - 문자열 s와 비스한 배렬(buffer)를 생성 => O(N)
69+
* > O(N)
70+
* Time Complexity: O(N)
71+
* - 문자열 s를 조회하면서 buffer에 붙이기 => O(N)
72+
* - buffer를 조회하면서 유효성 검사 => O(N)
73+
* > O(2N) => O(N)
74+
*/
75+
public boolean isPalindrome2(String s) {
76+
char a = 'a'; // 97
77+
char z = 'z'; // 122
78+
char A = 'A'; // 65
79+
char Z = 'Z'; // 90
80+
char zero = '0'; // 0
81+
char nine = '9'; // 9
82+
83+
String buffer = "";
84+
for (int i = 0; i < s.length(); i++) {
85+
char c = s.charAt(i);
86+
if ((a <= c && c <= z) || (zero <= c && c <= nine)) {
87+
buffer += c;
88+
} else if (A <= c && c <= Z) {
89+
char lowerC = (char) ((int) c + 32);
90+
buffer += lowerC;
91+
}
92+
}
93+
94+
// System.out.println(buffer);
95+
int leftIdx = 0;
96+
int rightIdx = buffer.length() - 1;
97+
while (leftIdx < rightIdx) {
98+
if (buffer.charAt(leftIdx) != buffer.charAt(rightIdx)) {
99+
return false;
100+
}
101+
leftIdx++;
102+
rightIdx--;
103+
}
104+
105+
return true;
106+
}
107+
}

0 commit comments

Comments
 (0)