23.06.09 Today’s Leetcode
744. Find Smallest Letter Greater Than Target (easy)
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
def compare(x, y):
n1, n2 = len(x), len(y)
n = max(len(x), len(y))
for i in range(n):
a = ord(x[i]) if i < n1 else 0
b = ord(y[i]) if i < n2 else 0
if a > b:
return 1
if a < b:
return -1
return 0
for letter in letters:
if compare(letter, target) > 0:
return letter
return letters[0]