Advertisement
Kali_prasad

A20250417b_google_lexographicMax

Apr 18th, 2025
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. '''
  2. A20250417b_google_lexographicMax
  3.  
  4. find the lexographically max subsequence that can be formed from the string but the
  5. twist each character in it should be unique
  6.  
  7. ======================================================
  8. important thing to be noted here is
  9.  
  10. first we need to understand the lexographic maximum
  11.  
  12. say abc
  13.  
  14. with this max string we can form is c
  15.  
  16. lets take cabcab
  17.  
  18. with this you can form cb
  19.  
  20. did you observe something there is some monotonocity in this
  21.  
  22. if you find monotonocity we can use kumar sir's stack approach
  23.  
  24. lets take a stack
  25.  
  26. traverse from left to right
  27.  
  28. for each character pop out while curr>=top (each char should be unique so that's why eliminating equals even)
  29.  
  30. at last put it on top
  31.  
  32.  
  33.  
  34.  
  35. =======================================================
  36. input -
  37. abzdczzc
  38.  
  39. output -
  40. zc
  41.  
  42. '''
  43.  
  44. arr = list(input())
  45. stack = []
  46. for charr in arr:
  47.     # print('charr is ',charr)
  48.     while len(stack)>0 and stack[-1]<=charr:
  49.         curr = stack.pop()
  50.         # print('curr is ',curr)
  51.     stack.append(charr)
  52.  
  53. final = ''
  54. while len(stack):
  55.     curr = stack.pop()
  56.     final = curr+final
  57.  
  58. print(final)
  59.  
  60.  
  61.  
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement