Given String str and an integer k, find the lexicographically smallest and largest substring of length k
Lexicography order, also called as alphabetical order or dictionary order,
A < B <... < Y < Z < a < b <.. < y < z
Input : String: hello Size: 2 Distinct Substring: [el, he, ll, lo] Output : Smallest Substring: el Largest Substring: lo Input : String: geeksforgeeks Size: 3 Distinct Substring: [eek, eks, for, gee, ksf, org, rge, sfo] Output : Smallest Substring: eek Largest Substring: sfo
We initialize max and min as first substring of size k. We traverse remaining substrings, by removing first character of previous substring and adding last character of new string. We keep track of the lexicographically largest and smallest.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest K-length substring containing maximum number of vowels
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Check if string can be made lexicographically smaller by reversing any substring
- Construct lexicographically smallest palindrome
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest rotated sequence | Set 2
- Lexicographically smallest string after M operations
- Lexicographically largest string possible in one swap
- Lexicographically largest sub-sequence of the given string
- Make a lexicographically smallest palindrome with minimal changes
- Lexicographically smallest permutation of a string with given subsequences
- Queries to answer the X-th smallest sub-string lexicographically
- Find lexicographically smallest string in at most one swaps
- Lexicographically smallest string formed by removing at most one character
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically smallest string obtained after concatenating array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.