Advertisement
Iam_Sandeep

Oracle modify string lexicographic leetcode Remove duplicates varient

Aug 3rd, 2022 (edited)
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class Solution:
  2.     def removeDuplicateLetters(self, s: str) -> str:
  3.         vis=set()
  4.         last = defaultdict(int)
  5.        
  6.         for i,j in enumerate(s): last[j]=i
  7.         st=[]
  8.         for i,j in enumerate(s):
  9.             if j not in vis:
  10.                 while st and last[st[-1]]>i and st[-1]<=j:
  11.                     vis.discard(st.pop())
  12.                 st.append(j)
  13.                 vis.add(j)
  14.         return ''.join(st)
  15.                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement