Skip to content

Commit 2d29799

Browse files
week10 mission longest-palindromic-substring
1 parent 1720f29 commit 2d29799

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
- 문제: https://leetcode.com/problems/longest-palindromic-substring/
2+
- 풀이: https://algorithm.jonghoonpark.com/2024/07/01/leetcode-5
3+
4+
## brute force 방식 (beats 12.41%)
5+
6+
```java
7+
class Solution {
8+
public String longestPalindrome(String s) {
9+
int start = 0;
10+
int end = start;
11+
12+
String max = "";
13+
14+
char[] charArray = s.toCharArray();
15+
while (true) {
16+
if (end == s.length()) {
17+
break;
18+
}
19+
20+
if (charArray[start] == charArray[end]) {
21+
int tempStart = start;
22+
int tempEnd = end;
23+
24+
boolean isPalindrome = true;
25+
while (tempEnd >= tempStart) {
26+
if (charArray[tempStart] != charArray[tempEnd]) {
27+
isPalindrome = false;
28+
break;
29+
}
30+
if (tempStart == tempEnd) {
31+
break;
32+
}
33+
tempStart = tempStart + 1;
34+
tempEnd = tempEnd - 1;
35+
}
36+
37+
if (isPalindrome) {
38+
String temp = s.substring(start, end + 1);
39+
if (temp.length() > max.length()) {
40+
max = temp;
41+
}
42+
}
43+
}
44+
45+
end++;
46+
if (end == s.length()) {
47+
start = start + 1;
48+
end = start;
49+
}
50+
51+
if (start == s.length()) {
52+
break;
53+
}
54+
}
55+
56+
return max;
57+
}
58+
}
59+
```
60+
61+
### TC, SC
62+
63+
μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n^2)이고, 곡간 λ³΅μž‘λ„λŠ” O(n)이닀.
64+
65+
## brute force 방식 κ°œμ„  (beats 48.52%)
66+
67+
μ•„λž˜ 뢀뢄이 뢀뢄이닀.
68+
69+
```java
70+
start = start + 1;
71+
end = start + max.length();
72+
```
73+
74+
λ‹€μŒ end 값을 `start + max.length()` 둜 두어 연산을 많이 쀄일 수 μžˆμ—ˆκ³  κ½€ 큰 차이가 λ°œμƒλœλ‹€.
75+
76+
```java
77+
class Solution {
78+
public String longestPalindrome(String s) {
79+
int start = 0;
80+
int end = start;
81+
82+
String max = "";
83+
84+
char[] charArray = s.toCharArray();
85+
while (start < s.length() && end < s.length()) {
86+
if (charArray[start] == charArray[end]) {
87+
int tempStart = start;
88+
int tempEnd = end;
89+
90+
boolean isPalindrome = true;
91+
while (tempEnd >= tempStart) {
92+
if (charArray[tempStart] != charArray[tempEnd]) {
93+
isPalindrome = false;
94+
break;
95+
}
96+
if (tempStart == tempEnd) {
97+
break;
98+
}
99+
tempStart = tempStart + 1;
100+
tempEnd = tempEnd - 1;
101+
}
102+
103+
if (isPalindrome) {
104+
String temp = s.substring(start, end + 1);
105+
if(temp.length() > max.length()) {
106+
max = temp;
107+
}
108+
end = end + 1;
109+
} else {
110+
if (s.indexOf(charArray[start], end) > -1) {
111+
end = end + 1;
112+
} else {
113+
start = start + 1;
114+
end = start + max.length();
115+
}
116+
}
117+
} else {
118+
end++;
119+
if (end == s.length()) {
120+
start = start + 1;
121+
end = start + max.length();
122+
}
123+
124+
if (start == s.length()) {
125+
break;
126+
}
127+
}
128+
129+
if (end == s.length()) {
130+
start = start + 1;
131+
end = start + max.length();
132+
}
133+
}
134+
135+
return max;
136+
}
137+
}
138+
```
139+
140+
### TC, SC
141+
142+
μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n^2)이고, 곡간 λ³΅μž‘λ„λŠ” O(n)이닀.
143+
144+
λΉ…μ˜€ ν‘œκΈ°λ²• μƒμœΌλ‘œλŠ” λ™μΌν•˜λ‚˜, μ‹€ν–‰ μ‹œκ°„μ΄ 맀우 λ‹¨μΆ•λ˜μ—ˆλ‹€.
145+
146+
## best solution (beats 95.84%)
147+
148+
ν•˜λ‚˜μ˜ 포인터λ₯Ό μ‚¬μš©ν•˜μ—¬, 각 문자λ₯Ό μ€‘μ‹¬μœΌλ‘œ Palindrome 이 λ°œμƒν•  수 μžˆλŠ” μΌ€μ΄μŠ€λ₯Ό μ‘°μ‚¬ν•œλ‹€.
149+
150+
```java
151+
class Solution {
152+
public String longestPalindrome(String s) {
153+
String max = "";
154+
155+
char[] charArray = s.toCharArray();
156+
for (int i = 0; i < s.length(); i++) {
157+
char currentChar = charArray[i];
158+
int left = i;
159+
int right = i;
160+
161+
while (right < s.length() - 1 && currentChar == charArray[right + 1]) {
162+
right++;
163+
}
164+
165+
while (left > 0 && right < s.length() - 1 && charArray[left - 1] == charArray[right + 1]) {
166+
left--;
167+
right++;
168+
}
169+
170+
String temp = s.substring(left, right + 1);
171+
if (temp.length() > max.length()) {
172+
max = temp;
173+
}
174+
}
175+
176+
return max;
177+
}
178+
}
179+
```
180+
181+
### TC, SC
182+
183+
μ‹œκ°„ λ³΅μž‘λ„λŠ” ν‰κ· μ μœΌλ‘œ O(n)이닀. palindrome 의 길이가 n 에 κ°€κΉŒμ›Œμ§ˆμˆ˜λ‘ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(n^2) 에 κ°€κΉŒμ›Œ μ§„λ‹€.
184+
곡간 λ³΅μž‘λ„λŠ” O(n)이닀.
185+
186+
λΉ…μ˜€ ν‘œκΈ°λ²• μƒμœΌλ‘œλ„ κ°œμ„ μ΄ 된 방식이닀. μ‹€μ œλ‘œ μ‹œκ°„λ„ 더 λ‹¨μΆ•λ˜μ—ˆλ‹€.

0 commit comments

Comments
Β (0)