Skip to content

Commit

Permalink
Add descriptions and cases for p5
Browse files Browse the repository at this point in the history
  • Loading branch information
yanqd0 committed Jun 23, 2019
1 parent a6e783b commit 792ed83
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,65 +26,45 @@
public class LongestPalindromicSubstring {
public String longestPalindrome(String s) {
final int len = s.length();
if (len <= 1) {
if (len < 2) {
return s;
}
int start = 0, end = 0;

int index = len / 2;
for (int step = 0; step < len; ++step) {
index += step % 2 == 1 ? step : -step;
int padding = Math.min(index, len - 1 - index);
if (end - start > 2 * padding) {
break;
}

boolean palindrom = true;
for (int half = 0; half <= padding; ++half) {
if (s.charAt(index + half) != s.charAt(index - half)) {
--half;
if (2 * half > end - start
|| 2 * half == end - start && index - half < start) {
start = index - half;
end = index + half;
}
palindrom = false;
int start = 0, end = 0;
for (int fix = 0; fix < 2; ++fix) {
int center = (len - 1) / 2;
for (int step = 0; step < len; ++step) {
center += (step + fix) % 2 == 1 ? step : -step;
int padding = Math.min(center, len - 1 - center - fix);
if (2 * padding + fix < end - start) {
break;
}
}
if (palindrom) {
start = index - padding;
end = index + padding;
}
}

index = len / 2;
for (int step = 0; step <= len; ++step) {
index += step % 2 == 1 ? step : -step;
int padding = Math.min(index - 1, len - 1 - index);
if (end - start > 2 * padding + 1) {
break;
}

boolean palindrom = true;
for (int half = 0; half <= padding; ++half) {
if (s.charAt(index - 1 - half) != s.charAt(index + half)) {
--half;
if (2 * half + 1 > end - start
|| 2 * half + 1 == end - start && index - 1 - half < start) {
start = index - 1 - half;
end = index + half;
boolean palindrom = true;
for (int half = 0; half <= padding; ++half) {
if (s.charAt(center - half) != s.charAt(center + fix + half)) {
--half;
if (2 * half + fix > end - start
|| 2 * half + fix == end - start && center - 1 - half < start) {
start = center - half;
end = center + fix + half;
}
palindrom = false;
break;
}
palindrom = false;
break;
}
}
if (palindrom) {
start = index - 1 - padding;
end = index + padding;

if (palindrom) {
start = center - padding;
end = center + fix + padding;
}
}
}

return s.substring(start, end + 1);
}
}

/*
Runtime: 6 ms, faster than 88.02% of Java online submissions for Longest Palindromic Substring.
Memory Usage: 36 MB, less than 99.98% of Java online submissions for Longest Palindromic Substring.
*/
73 changes: 31 additions & 42 deletions python/src/leetcode/longest_palindromic_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,39 @@

# noinspection PyPep8Naming,PyMethodMayBeStatic
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
def longestPalindrome(self, s: str) -> str:
start = end = 0
str_len = len(s)

# odd palindrome
index = (str_len - 1) // 2
for step in range(str_len):
index += step if step % 2 else -step
padding = min(index, str_len - 1 - index)
if 2 * padding < end - start:
break

for half in range(padding + 1):
if s[index - half] != s[index + half]:
half -= 1
if end - start < 2 * half or (
end - start == 2 * half and index - half < start
):
start, end = index - half, index + half
length = len(s)
for fix in range(2):
center = (length - 1) // 2
for step in range(length):
center += step if (step + fix) % 2 else -step
padding = min(center, length - 1 - (center + fix))
if 2 * padding + fix < end - start:
break
else:
start, end = index - padding, index + padding

# even palindrome
index = (str_len - 1) // 2
for step in range(str_len):
index -= step if step % 2 else -step
padding = min(index, str_len - 1 - (index + 1))
if 2 * padding + 1 < end - start:
break
for half in range(padding + 1):
if s[center - half] != s[center + fix + half]:
half -= 1
if end - start < 2 * half + fix or (
end - start == 2 * half + fix
and center - half < start
):
start, end = center - half, center + fix + half
break
else:
start, end = center - padding, center + fix + padding
return s[start:end + 1]

for half in range(padding + 1):
if s[index - half] != s[index + 1 + half]:
half -= 1
if end - start < 2 * half + 1 or (
end - start == 2 * half + 1 and index - half < start
):
start, end = index - half, index + 1 + half
break
else:
start, end = index - padding, index + 1 + padding

return s[start:end + 1]
# Python 2
# Runtime: 204 ms, faster than 90.21% of Python online submissions
# for Longest Palindromic Substring.
# Memory Usage: 11.8 MB, less than 57.96% of Python online submissions
# for Longest Palindromic Substring.
#
# Python 3
# Runtime: 132 ms, faster than 90.76% of Python3 online submissions
# for Longest Palindromic Substring.
# Memory Usage: 13.2 MB, less than 63.56% of Python3 online submissions
# for Longest Palindromic Substring.
26 changes: 14 additions & 12 deletions test/cases/longest_palindromic_substring.csv
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
input_str,expect
a,a
abb,bb
ccd,cc
cbbd,bb
aaaab,aaaa
abbbb,bbbb
babad,bab
aaaaab,aaaaa
abbbbb,bbbbb
ababababccd,abababa
abcdefgfedcba,abcdefgfedcba
abcdefg,a
"",""
"a","a"
"abb","bb"
"ccd","cc"
"cbbd","bb"
"aaaab","aaaa"
"abbbb","bbbb"
"babad","bab"
"aaaaab","aaaaa"
"abbbbb","bbbbb"
"ababababccd","abababa"
"abcdefgfedcba","abcdefgfedcba"
"abcdefg","a"
"aaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaa"
4 changes: 3 additions & 1 deletion test/generators/longest_palindromic_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

fields = ['input_str', 'expect']
rows = [
('', ''),
('a', 'a'),
('abb', 'bb'),
('ccd', 'cc'),
Expand All @@ -15,9 +16,10 @@
('ababababccd', 'abababa'),
('abcdefgfedcba', 'abcdefgfedcba'),
('abcdefg', 'a'),
('aaaaaaaaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaaaaaaaa'),
]

if __name__ == '__main__':
from utils import generate_csv

generate_csv(__file__, fields, rows)
generate_csv(__file__, fields, rows, quote_empty=True)

0 comments on commit 792ed83

Please sign in to comment.