Skip to content

Commit 56e12fd

Browse files
committed
Apply review
1 parent b2bc838 commit 56e12fd

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

โ€Žlongest-palindromic-substring/bky373.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ private boolean isPalindrome(String s) {
3535
}
3636

3737
/*
38-
* Approach 2.
39-
* time: O(n^2)
38+
* Approach 2-1.
39+
* time: ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ํ‰๊ท ์ ์œผ๋กœ O(n)์ด๊ณ , palindrome ์˜ ๊ธธ์ด๊ฐ€ n ์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„ ์—ญ์‹œ O(n^2) ์— ๊ฐ€๊นŒ์›Œ ์ง„๋‹ค.
4040
* space: O(1)
4141
*/
4242
class Solution {
@@ -71,4 +71,34 @@ public String longestPalindrome(String s) {
7171
return s.substring(maxStart, maxEnd + 1);
7272
}
7373
}
74+
75+
/*
76+
* Approach 2-2.
77+
* time: ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ํ‰๊ท ์ ์œผ๋กœ O(n)์ด๊ณ , palindrome ์˜ ๊ธธ์ด๊ฐ€ n ์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„ ์—ญ์‹œ O(n^2) ์— ๊ฐ€๊นŒ์›Œ ์ง„๋‹ค.
78+
* space: O(1)
79+
*/
80+
class Solution {
81+
int maxStart = 0;
82+
int maxEnd = 0;
83+
84+
public String longestPalindrome(String s) {
85+
for (int i = 0; i < s.length(); i++) {
86+
calculateMaxLength(i, i, s);
87+
calculateMaxLength(i, i+1, s);
88+
}
89+
return s.substring(maxStart, maxEnd + 1);
90+
}
91+
92+
public void calculateMaxLength(int start, int end, String s){
93+
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
94+
if (this.maxEnd - this.maxStart < end - start) {
95+
this.maxStart = start;
96+
this.maxEnd = end;
97+
}
98+
start--;
99+
end++;
100+
}
101+
}
102+
103+
}
74104
}

0 commit comments

Comments
ย (0)