Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 516 Bytes

942.md

File metadata and controls

24 lines (22 loc) · 516 Bytes

942. DI String Match

Solution 1 (time O(n), space O(1))

class Solution(object):
    def diStringMatch(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        n = len(s)
        p1, p2 = 0, n
        ans = []
        for i in range(len(s)):
            if s[i] == 'I':
                ans.append(p1)
                p1 += 1
            else:
                ans.append(p2)
                p2 -= 1
        ans.append(p1)
        return ans