given a list of sorted characters letters containing only lowercase letters and given a target letter target find the smallest element in the list that is larger than the given target letters also wrap around for example if the target is target z and letters a b the answer is a input letters c f j target a output c input letters c f j target c output f input letters c f j target d output f reference https leetcode comproblemsfindsmallestlettergreaterthantargetdescription using bisect libarary using binary search complexity ologn brute force complexity on using bisect libarary using binary search complexity o logn brute force complexity o n
import bisect

def next_greatest_letter(letters, target):
    
    index = bisect.bisect(letters, target)
    return letters[index % len(letters)]

def next_greatest_letter_v1(letters, target):
    
    if letters[0] > target:
        return letters[0]
    if letters[len(letters) - 1] <= target:
        return letters[0]
    left, right = 0, len(letters) - 1
    while left <= right:
        mid = left + (right - left) // 2
        if  letters[mid] > target:
            right = mid - 1
        else:
            left = mid + 1
    return letters[left]

def next_greatest_letter_v2(letters, target):
    
    for index in letters:
        if index > target:
            return index
    return letters[0]