• + 0 comments

    Python solution

    def find_smallest_char_larger(s, idx):
        char_compare = s[idx]
        sorted_chars = sorted(s[(idx + 1):])
        for char in sorted_chars:
            if char > char_compare:
                i = s.index(char, idx)
                return i
        return None
        
    def biggerIsGreater(w):
        # Write your code here
        w_list = list(w)
        
        for idx in reversed(range(0, len(w) - 1)):
            char_i_to_swap = find_smallest_char_larger(w_list, idx)
            if char_i_to_swap is not None:
                w_list[char_i_to_swap], w_list[idx] = w_list[idx], w_list[char_i_to_swap]
                w_list[(idx + 1):] = sorted(w_list[(idx + 1):])
                return ''.join(w_list)
        
        return 'no answer'