Python O(n^2)


  • 0
    class Solution(object):
        def findPermutation(self, s):
            l = len(s)
            ans = [i + 1 for i in range(l + 1)]
            for i in range(l):
                if s[i] == 'D':
                    ans[i + 1], ans[i] = ans[i], ans[i + 1]
                    j = i - 1
                    while j >= 0 and s[j] == 'D':
                        ans[j + 1], ans[j] = ans[j], ans[j + 1]
                        j -= 1
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.