...
Code Block | ||
---|---|---|
| ||
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not strs: return "" result="" cnt=len(strs) for i in range(len(strs[0])): j=1 while j<cnt and i<len(strs[j]) and strs[j][i]==strs[0][i]: j+=1 if j==cnt: result += strs[0][i] else: break; return result; |
Longest Palindromic Substring
Info |
---|
Input: "babad" → Output: "bab" Input: "cbd" → Output: "bb" |
Code Block |
---|
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
# Return if string is empty
if not s: return s
res = ""
for i in range(len(s)):
j = i + 1
# While j is less than length of string
# AND res is *not* longer than substring s[i:]
while j <= len(s) and len(res) <= len(s[i:]):
# If substring s[i:j] is a palindrome
# AND substring is longer than res
if s[i:j] == s[i:j][::-1] and len(s[i:j]) > len(res):
res = s[i:j]
j += 1
return res |