fork download
  1. def smallestNumber(pattern: str) -> str:
  2. stack = []
  3. result = []
  4.  
  5. # Iterate through the pattern and the last number
  6. for i in range(len(pattern) + 1):
  7. # Push the current number to the stack
  8. stack.append(str(i + 1))
  9.  
  10. # If we reach the end or the current pattern is 'I', pop all from stack
  11. if i == len(pattern) or pattern[i] == 'I':
  12. while stack:
  13. result.append(stack.pop())
  14.  
  15. return ''.join(result)
  16.  
  17. print(smallestNumber("IIIDIDDD"))
Success #stdin #stdout 0.08s 14172KB
stdin
45
stdout
123549876