Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- A20250417b_google_lexographicMax
- find the lexographically max subsequence that can be formed from the string but the
- twist each character in it should be unique
- ======================================================
- important thing to be noted here is
- first we need to understand the lexographic maximum
- say abc
- with this max string we can form is c
- lets take cabcab
- with this you can form cb
- did you observe something there is some monotonocity in this
- if you find monotonocity we can use kumar sir's stack approach
- lets take a stack
- traverse from left to right
- for each character pop out while curr>=top (each char should be unique so that's why eliminating equals even)
- at last put it on top
- =======================================================
- input -
- abzdczzc
- output -
- zc
- '''
- arr = list(input())
- stack = []
- for charr in arr:
- # print('charr is ',charr)
- while len(stack)>0 and stack[-1]<=charr:
- curr = stack.pop()
- # print('curr is ',curr)
- stack.append(charr)
- final = ''
- while len(stack):
- curr = stack.pop()
- final = curr+final
- print(final)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement