Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def solution(s: str) -> str:
- # initialize a list to record the output. (A list because it'll be easier to manipulate later)
- output = []
- idx = 0
- while idx < len(s):
- # initialize a temporary list to store the current sequence
- temp = [s[idx]]
- idx += 1
- # keep advancing while we're in bounds and the current and previous characters are consecutive
- while idx < len(s) and ord(s[idx]) - ord(s[idx-1]) == 1:
- temp.append(s[idx])
- idx += 1
- # if the while loop ended and we didn't add any letters, just add the letter we were on
- # Otherwise, extend the output with the reverse of the temporary list
- if len(temp) > 1:
- output.extend(temp[::-1])
- else:
- output.extend(temp)
- # we have to return a string, so join the output list
- return "".join(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement