classSolution: defdiStringMatch(self, S): n = len(S) left = 0 right = n result = [] for i in range(n + 1): if i == n or S[i] == 'I': result.append(left) left += 1 else: result.append(right) right -= 1 return result
Version 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defdiStringMatch(self, S): left = 0 right = len(S) result = [] for ch in S: if ch == 'I': result.append(left) left += 1 else: result.append(right) right -= 1 result.append(left) return result