Advertisement
TwentyEight

> < ~

Mar 7th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. class Solution:
  2.     def sortString(self, s: str) -> str:
  3.         ans = ''
  4.         while (len(s) > 0): # step 7: repeat
  5.             ans2, s = self.getSmall(s) #step 1-3
  6.             ans += ans2
  7.             if (len(s) == 0):
  8.                 return ans
  9.             ans2, s = self.getBig(s) # step 4-6
  10.             ans += ans2
  11.         return ans
  12.        
  13.     def getSmall(self, s):
  14.         min_s = min(s)
  15.         ans = min_s # step 1
  16.         i = s.index(min_s)
  17.         s2 = s[:i] + s[(i+1):]
  18.         s = s.replace(min_s, '')
  19.         while (len(s) > 0): # step 3: repeat
  20.             min_s = min(s)  
  21.             ans += min_s # step 2
  22.             i = s2.index(min_s)
  23.             s2 = s2[:i] + s2[(i+1):]
  24.             s = s.replace(min_s, '')
  25.         return ans, s2
  26.    
  27.     def getBig(self, s):
  28.         max_s = max(s) # step 4
  29.         ans = max_s
  30.         i = s.index(max_s)
  31.         s2 = s[:i] + s[(i+1):]
  32.         s = s.replace(ans[-1], '')
  33.         while (len(s) > 0): # step 6: repeat
  34.             max_s = max(s)
  35.             ans += max_s # step 5
  36.             i = s2.index(max_s)
  37.             s2 = s2[:i] + s2[(i+1):]
  38.             s = s.replace(max_s, '')
  39.         return ans, s2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement