String
Introduction
Each problem on the platform includes a direct solution, while others solely outline the approach within a function.
firstPalindrome(words)
Parameters:
Name |
Type |
Description |
Default |
words |
List[string]
|
|
required
|
output |
string
|
|
required
|
Source code in algorithms/strings/firstpalindrome.py
| def firstPalindrome(words):
"""
Args:
words List[string] : Input
output string : Output
"""
for word in words:
if reverse(word) == word:
return word
return ""
|
Solution
Source code in algorithms/strings/longestSubstring.py
| class Solution:
def lengthLongestSubsequence(self,s):
"""
:type s: str
:rtype : int
"""
i = j = max_len = 0
substring = set()
while i < len(s) and j < len(s):
if s[i] not in substring:
substring.add(s[i])
max_len = max(max_len, i-j+1)
i+=1
substring.remove(s[j])
j+=1
return max_len
|
lengthLongestSubsequence(s)
:type s: str
:rtype : int
Source code in algorithms/strings/longestSubstring.py
| def lengthLongestSubsequence(self,s):
"""
:type s: str
:rtype : int
"""
i = j = max_len = 0
substring = set()
while i < len(s) and j < len(s):
if s[i] not in substring:
substring.add(s[i])
max_len = max(max_len, i-j+1)
i+=1
substring.remove(s[j])
j+=1
return max_len
|
Solution
Source code in algorithms/strings/validAnagram.py
| class Solution:
def validAnagram(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype : bool
"""
if len(s1) == len(s2):
def helper(str):
char_count = {}
for s in str:
if s in char_count:
char_count[s] +=1
else:
char_count[s] = 1
return char_count
return helper(s1) == helper(s2)
return False
|
validAnagram(s1, s2)
:type s1: str
:type s2: str
:rtype : bool
Source code in algorithms/strings/validAnagram.py
| def validAnagram(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype : bool
"""
if len(s1) == len(s2):
def helper(str):
char_count = {}
for s in str:
if s in char_count:
char_count[s] +=1
else:
char_count[s] = 1
return char_count
return helper(s1) == helper(s2)
return False
|
Solution
Bases: object
Source code in algorithms/strings/validparenthesis.py
| class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
mapping = {
')':'(',
'}':'{',
"]":'['
}
stack = []
for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)
return not stack
|
isValid(s)
:type s: str
:rtype: bool
Source code in algorithms/strings/validparenthesis.py
| def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
mapping = {
')':'(',
'}':'{',
"]":'['
}
stack = []
for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)
return not stack
|
Solution
Source code in algorithms/strings/maxScore.py
| class Solution:
def maxScore(self,s):
"""
:type s: str
:rtype : int
"""
if s and len(s)>1:
max_score = 0
for i in range(1, len(s)):
left = s[:i]
right = s[i:]
score = left.count('0') + right.count('1')
max_score = max(score, max_score)
return max_score
return max(s.count('0'),s.count('1'))
|
maxScore(s)
:type s: str
:rtype : int
Source code in algorithms/strings/maxScore.py
| def maxScore(self,s):
"""
:type s: str
:rtype : int
"""
if s and len(s)>1:
max_score = 0
for i in range(1, len(s)):
left = s[:i]
right = s[i:]
score = left.count('0') + right.count('1')
max_score = max(score, max_score)
return max_score
return max(s.count('0'),s.count('1'))
|
Solution
Bases: object
Source code in algorithms/strings/reverseString.py
| class Solution(object):
def findRelativeRanks(self, score):
"""
:type score: List[int]
:rtype: List[str]
"""
places = ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
n = len(score)
a = [" "]*n
d = defaultdict(int)
for i in range(n):
d[score[i]] = i
score.sort(reverse=True)
for i in range(n):
if i < 3:
a[d[score[i]]] = places[i]
else:
a[d[score[i]]] = str(i+1)
return a
|
findRelativeRanks(score)
:type score: List[int]
:rtype: List[str]
Source code in algorithms/strings/reverseString.py
| def findRelativeRanks(self, score):
"""
:type score: List[int]
:rtype: List[str]
"""
places = ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
n = len(score)
a = [" "]*n
d = defaultdict(int)
for i in range(n):
d[score[i]] = i
score.sort(reverse=True)
for i in range(n):
if i < 3:
a[d[score[i]]] = places[i]
else:
a[d[score[i]]] = str(i+1)
return a
|