Skip to content

Commit f13a7b1

Browse files
add post '(Leetcode) 5 - Longest Palindromic Substring'
1 parent a88453c commit f13a7b1

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

_posts/2024-07-01-leetcode-5.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
layout: post
3+
title: (Leetcode) 5 - Longest Palindromic Substring
4+
categories: [스터디-알고리즘]
5+
tags: [자바, java, 리트코드, Leetcode, 알고리즘, brute force, array, pointer]
6+
date: 2024-07-01 19:30:00 +0900
7+
toc: true
8+
---
9+
10+
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.
11+
12+
[https://neetcode.io/practice](https://neetcode.io/practice)
13+
14+
---
15+
16+
[https://leetcode.com/problems/longest-palindromic-substring](https://leetcode.com/problems/longest-palindromic-substring)
17+
18+
점진적으로 개선하여 보았다.
19+
20+
## brute force 방식 (beats 12.41%)
21+
22+
```java
23+
class Solution {
24+
public String longestPalindrome(String s) {
25+
int start = 0;
26+
int end = start;
27+
28+
String max = "";
29+
30+
char[] charArray = s.toCharArray();
31+
while (true) {
32+
if (end == s.length()) {
33+
break;
34+
}
35+
36+
if (charArray[start] == charArray[end]) {
37+
int tempStart = start;
38+
int tempEnd = end;
39+
40+
boolean isPalindrome = true;
41+
while (tempEnd >= tempStart) {
42+
if (charArray[tempStart] != charArray[tempEnd]) {
43+
isPalindrome = false;
44+
break;
45+
}
46+
if (tempStart == tempEnd) {
47+
break;
48+
}
49+
tempStart = tempStart + 1;
50+
tempEnd = tempEnd - 1;
51+
}
52+
53+
if (isPalindrome) {
54+
String temp = s.substring(start, end + 1);
55+
if (temp.length() > max.length()) {
56+
max = temp;
57+
}
58+
}
59+
}
60+
61+
end++;
62+
if (end == s.length()) {
63+
start = start + 1;
64+
end = start;
65+
}
66+
67+
if (start == s.length()) {
68+
break;
69+
}
70+
}
71+
72+
return max;
73+
}
74+
}
75+
```
76+
77+
### TC, SC
78+
79+
시간 복잡도는 O(n^2)이고, 공간 복잡도는 O(n)이다.
80+
81+
![approach1_submit_result](/assets/images/2024-07-01-leetcode-5/approach1_submit_result.png)
82+
83+
## brute force 방식 개선 (beats 48.52%)
84+
85+
아래 부분이 부분이다.
86+
87+
```java
88+
start = start + 1;
89+
end = start + max.length();
90+
```
91+
92+
end 를 `start + max.length()` 로 두어 연산을 많이 줄일 수 있었고 꽤 큰 차이가 발생된다.
93+
94+
```java
95+
class Solution {
96+
public String longestPalindrome(String s) {
97+
int start = 0;
98+
int end = start;
99+
100+
String max = "";
101+
102+
char[] charArray = s.toCharArray();
103+
while (start < s.length() && end < s.length()) {
104+
if (charArray[start] == charArray[end]) {
105+
int tempStart = start;
106+
int tempEnd = end;
107+
108+
boolean isPalindrome = true;
109+
while (tempEnd >= tempStart) {
110+
if (charArray[tempStart] != charArray[tempEnd]) {
111+
isPalindrome = false;
112+
break;
113+
}
114+
if (tempStart == tempEnd) {
115+
break;
116+
}
117+
tempStart = tempStart + 1;
118+
tempEnd = tempEnd - 1;
119+
}
120+
121+
if (isPalindrome) {
122+
String temp = s.substring(start, end + 1);
123+
if(temp.length() > max.length()) {
124+
max = temp;
125+
}
126+
end = end + 1;
127+
} else {
128+
if (s.indexOf(charArray[start], end) > -1) {
129+
end = end + 1;
130+
} else {
131+
start = start + 1;
132+
end = start + max.length();
133+
}
134+
}
135+
} else {
136+
end++;
137+
if (end == s.length()) {
138+
start = start + 1;
139+
end = start + max.length();
140+
}
141+
142+
if (start == s.length()) {
143+
break;
144+
}
145+
}
146+
147+
if (end == s.length()) {
148+
start = start + 1;
149+
end = start + max.length();
150+
}
151+
}
152+
153+
return max;
154+
}
155+
}
156+
```
157+
158+
### TC, SC
159+
160+
시간 복잡도는 O(n^2)이고, 공간 복잡도는 O(n)이다.
161+
162+
빅오 표기법 상으로는 동일하나, 실행 시간이 매우 단축되었다.
163+
164+
![approach2_submit_result](/assets/images/2024-07-01-leetcode-5/approach2_submit_result.png)
165+
166+
## best solution (beats 95.84%)
167+
168+
하나의 포인터를 사용하여, 각 문자를 중심으로 Palindrome 이 발생할 수 있는 케이스를 조사한다.
169+
170+
```java
171+
class Solution {
172+
public String longestPalindrome(String s) {
173+
String max = "";
174+
175+
char[] charArray = s.toCharArray();
176+
for (int i = 0; i < s.length(); i++) {
177+
char currentChar = charArray[i];
178+
int left = i;
179+
int right = i;
180+
181+
while (right < s.length() - 1 && currentChar == charArray[right + 1]) {
182+
right++;
183+
}
184+
185+
while (left > 0 && right < s.length() - 1 && charArray[left - 1] == charArray[right + 1]) {
186+
left--;
187+
right++;
188+
}
189+
190+
String temp = s.substring(left, right + 1);
191+
if (temp.length() > max.length()) {
192+
max = temp;
193+
}
194+
}
195+
196+
return max;
197+
}
198+
}
199+
```
200+
201+
### TC, SC
202+
203+
시간 복잡도는 평균적으로 O(n)이다. palindrome 의 길이가 n 에 가까워질수록 시간 복잡도는 O(n^2) 에 가까워 진다.
204+
공간 복잡도는 O(n)이다.
205+
206+
빅오 표기법 상으로도 개선이 된 방식이다. 실제로 시간도 더 단축되었다.
207+
208+
![approach3_submit_result](/assets/images/2024-07-01-leetcode-5/approach3_submit_result.png)
113 KB
Loading
114 KB
Loading
114 KB
Loading

0 commit comments

Comments
 (0)