Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Created September 12, 2016 00:16
Show Gist options
  • Save Swimburger/4274f8e2bc6309d9bf20ed1ef105ccd8 to your computer and use it in GitHub Desktop.
Save Swimburger/4274f8e2bc6309d9bf20ed1ef105ccd8 to your computer and use it in GitHub Desktop.
Find longest ordered substring in Python
longestOrderedPart = ''
randomString = 'ljdflkjdfabcdljfbcdxyzlsjmlkjqsmfslkjfqllldflsdlfsldmlsqmslfdk'
orderedPart = randomString[0]
# loop over randomString
for i in range(0, len(randomString) -1): #don't run over last character or nextCharacter will go outside of the bounds of the string
currentCharacter = randomString[i]
nextCharacter = randomString[i+1]
#if nextCharacter is bigger or equal than currentCharacter, than it was in correct order
#in that case add character to orderedPart
if nextCharacter >= currentCharacter:
orderedPart += nextCharacter
#else reset the orderedPart
else:
count = 0
orderedPart = nextCharacter
#if the temporary orderedPart is longer than the longestOrderedPart, set the new orderedPart to the longestOrderedPart
if len(orderedPart) > len(longestOrderedPart):
longestOrderedPart = orderedPart
#print out the longestOrderedPart
print(longestOrderedPart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment