fork(3) download
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7.  
  8. """
  9. This solution works in O(N * 26 + N) [in worst case] which is better then
  10. O(N2)
  11. """
  12.  
  13. #s = 'edxeducation'
  14. #s = 'deepanshu' #My name :)
  15. #s = 'ghaaawxyzijbbbklccc'
  16. s = 'abcdefghijklmnopqrstuvwxyzabcduuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
  17. lst = [[] for i in range(26)]
  18.  
  19.  
  20. for ch in s:
  21.  
  22. ml = 0
  23.  
  24. # print ord(ch) - ord('a') #Debug it :-/ I know you nailed it ;)
  25.  
  26. for i in range(0,ord(ch) + 1 - ord('a')):
  27.  
  28. # k = len(lst[i]) #Debug it :-/ I know you nailed it ;)
  29.  
  30. if len(lst[i]) > len(lst[ml]):
  31. ml= i
  32.  
  33. cpy = ''.join(lst[ml])
  34.  
  35. #lst[ord(ch) - ord('a')] = ''.join(lst[ord(ch) - ord('a')]) + ch
  36. #Debug it :-/ I know you nailed it ;)
  37.  
  38. lst[ord(ch) - ord('a')] = cpy + ch
  39.  
  40.  
  41. ml = 0
  42. for i in range(26):
  43. if len(lst[i]) > len(lst[ml]):
  44. ml = i
  45. print lst[ml]
Success #stdin #stdout 0.01s 7736KB
stdin
Standard input is empty
stdout
abcdefghijklmnopqrstuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu