• + 0 comments
    def abbreviation(a, b):
        # Write your code here
        results, valid, last = {''}, False, False
        for i, char in enumerate(a):
            new = set()
            last = i == len(a) - 1
            for result in results:
                less = len(result) < len(b)
                if char.isupper():
                    if last: valid = result + char == b
                    elif less: new.add(result + char)
                else:
                    temp = result + char.upper()
                    if last: valid = b == result or b == temp
                    if not last:
                        new.add(result)
                        if less and b.startswith(temp): new.add(temp)
                if last and valid: return 'YES'
            results = new
        return 'NO'