Remove Duplicate Letters

Problem: Remove Duplicate Letters

In our solution, we first sort the letters and start from the first one. We find this letter in the original string and check whether its following sub-string contains all necessary letters. If so, we can append the letter to our answer and deal with the following sub-string with the same method. Otherwise, we can assume next letter in order is a proper one to append.

Code in Python:

# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

class Solution(object):
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        i, j = 1, n
        while i < j:
            if isBadVersion(i): return i
            if isBadVersion((i+j)/2):
                i += 1
                j = (i+j)/2
            else:
                i = (i+j)/2+1
        return is

results matching ""

    No results matching ""