Skip to content

Commit 9044b1d

Browse files
committed
Leetcode 5
Python solution
1 parent cea1212 commit 9044b1d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages!!
1111
| 2 | Medium | [Add Two Numbers](leetcode/2-Medium-Add-Two-Numbers/problem.md) | [C](leetcode/2-Medium-Add-Two-Numbers/answer.c) |
1212
| 3 | Medium | [Longest Substring Without Repeating Characters](leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/problem.md) | [Python](leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/answer.py) |
1313
| 4 | Hard | [Median of Two Sorted Arrays](leetcode/4-Hard-Median-Of-Two-Sorted-Arrays/problem.md) | [Python](leetcode/4-Hard-Median-Of-Two-Sorted-Arrays/answer.py) |
14+
| 5 | Medium | [Longest Palindromic Substring](leetcode/5-Medium-Longest-Palindromic-Substring/problem.md) | [Python](leetcode/5-Medium-Longest-Palindromic-Substring/answer.py) |
1415
| 6 | Medium | [Zig Zag Conversion](leetcode/6-Medium-Zig-Zag-Conversion/problem.md) | [Java](leetcode/6-Medium-Zig-Zag-Conversion/answer.java) |
1516
| 7 | Easy | [Reverse Integer](leetcode/7-Easy-Reverse-Integer/problem.md) | [Python](leetcode/7-Easy-Reverse-Integer/answer.py) |
1617
| 9 | Easy | [Palindrome Number](leetcode/9-Easy-Palindrome-Number/problem.md) | [C](leetcode/9-Easy-Palindrome-Number/answer.c), [Java](leetcode/9-Easy-Palindrome-Number/answer.java), [Python](leetcode/9-Easy-Palindrome-Number/answer.py), [Swift](leetcode/9-Easy-Palindrome-Number/answer.swift) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
class Solution:
4+
def longestPalindrome(self, s):
5+
"""
6+
:type s: str
7+
:rtype: str
8+
"""
9+
10+
result = ""
11+
for i in range(len(s)):
12+
# Takes in account odd and even cases
13+
result = max(result, self.find_palindrome(s, i, i), self.find_palindrome(s, i, i+1), key=len)
14+
15+
return result
16+
17+
# Helper method to find the longest palindrome starting from the center
18+
def find_palindrome(self, s, left, right):
19+
while left >= 0 and right < len(s) and s[left] == s[right]:
20+
left -= 1
21+
right += 1
22+
return s[left+1:right]
23+
24+
#-------------------------------------------------------------------------------
25+
#Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Problem 3: Longest Substring Without Repeating Characters
2+
3+
4+
#### Difficulty: Medium
5+
6+
#### Problem
7+
8+
Given a string, find the length of the longest substring without repeating
9+
characters.
10+
11+
#### Example
12+
13+
<pre>
14+
15+
Given "abcabcbb", the answer is "abc", which the length is 3.
16+
17+
Given "bbbbb", the answer is "b", with the length of 1.
18+
19+
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer
20+
must be a substring, "pwke" is a subsequence and not a substring.
21+
22+
/pre>

0 commit comments

Comments
 (0)