Skip to content

Instantly share code, notes, and snippets.

@pallabpain
Created November 27, 2021 16:39
Show Gist options
  • Save pallabpain/b207d35f544c2a04ee27c322e97ce42b to your computer and use it in GitHub Desktop.
Save pallabpain/b207d35f544c2a04ee27c322e97ce42b to your computer and use it in GitHub Desktop.
Given a string S, print the longest substring P such that P > S lexicographically.
def longestSubstr(s):
for i in range(1, len(s)):
if s[i] > s[0]:
break
# For cases like, AABAB
# BAB is lexicographically greater, however,
# ABAB is the longest such substring
while i > 1 and s[i-1] == s[0]:
i -= 1
return s[i:]
if __name__ == "__main__":
print(longestSubstr("AABAB") == "ABAB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment