942. DI String Match
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : February 18, 2025
Last updated : February 18, 2025
Related Topics : Array, Two Pointers, String, Greedy
Acceptance Rate : 79.86 %
class Solution:
def diStringMatch(self, s: str) -> List[int]:
minn, maxx = 0, 0
output = [0]
for c in s :
if c == 'I' :
output.append(maxx := maxx + 1)
else :
output.append(minn := minn - 1)
return [x - minn for x in output]
class Solution:
def diStringMatch(self, s: str) -> List[int]:
minn, maxx = 0, 0
output = [0] + [(maxx := maxx + 1) if c == 'I' else (minn := minn - 1) for c in s]
return [x - minn for x in output]
class Solution:
def diStringMatch(self, s: str) -> List[int]:
return [(l := 0 if 'l' not in locals() else l + 1) if c == 'I' else (r := len(s) if 'r' not in locals() else r - 1) for c in s] + [l + 1 if 'l' in locals() else r - 1]