Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. s = 'abcabcabc'
  2. i = 0
  3. j = 1
  4. longestSubString = ''
  5. realString = ''
  6. while (j < len(s)):
  7. if i == 0:
  8. longestSubString = s[i]
  9. if (s[j] >= s[i]):
  10. longestSubString = longestSubString + s[i]
  11. if len(longestSubString) > len (realString):
  12. realString = longestSubString
  13. i += 1
  14. j += 1
  15. else:
  16. longestSubString = ''
  17. i += 1
  18. j += 1
  19. print ("Longest SubString is: " + realString)
  20.  
  21. import string
  22.  
  23. s = 'azcbobobegghakl'
  24. i = 0
  25. currentSubString = ''
  26. longestSubString = ''
  27.  
  28. while (i < len(s)):
  29.  
  30. positionCurrent = string.ascii_lowercase.index(s[i])
  31. positionPrevious = string.ascii_lowercase.index(s[i-1])
  32. currentCharacter = s[i]
  33. i += 1
  34.  
  35. if (positionCurrent != positionPrevious + 1):
  36. currentSubString = ''
  37.  
  38. currentSubString += currentCharacter
  39.  
  40. if len(longestSubString) < len(currentSubString):
  41. longestSubString = currentSubString
  42.  
  43. print("Longest SubString is: " + longestSubString)
  44.  
  45. s = 'azcbobobegghakl'
  46. res = ''
  47. tmp = ''
  48.  
  49. for i in range(len(s)):
  50. tmp += s[i]
  51. if len(tmp) > len(res):
  52. res = tmp
  53. if i > len(s)-2:
  54. break
  55. if s[i] > s[i+1]:
  56. tmp = ''
  57.  
  58. print("Longest substring in alphabetical order is: {}".format(res))
Add Comment
Please, Sign In to add comment