Created
November 27, 2021 16:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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