Problem statement

https://binarysearch.com/problems/Lexicographically-Bigger-String/

Solution

We need to sort string and compare: it can be proved that this greedy strategy works.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, s, t):
        s, t = sorted(s), sorted(t)
        if sum(x < y for x, y in zip(s, t)) == 0: return True
        if sum(x < y for x, y in zip(t, s)) == 0: return True
        return False