Given a string, find a prefix with the highest frequency. If two prefixes have the same frequency then consider the one with maximum length.
Input : str = "abc" Output : abc Each prefix has same frequency(one) and the prefix with maximum length is "abc". Input : str = "abcab" Output : ab Both prefix "a" and "ab" occur two times and the prefix with maximum length is "ab".
The idea is to observe that every prefix of the given string will contain the first character of the string in it and the first character alone is also a prefix of the given string. So the prefix with the highest occurrence is the first character. The task now remains to maximize the length of the highest frequency prefix.
- Take a vector which will store the indices of the first element of the string.
- If the first element appeared only once, then the longest prefix will be the whole string.
- Otherwise, loop till the second appearance of the first element and check one letter after every stored index.
- If there is no mismatch we move forward otherwise we stop.
Below is the implementation of the above approach:
Time Complexity : O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Longest string in an array which matches with prefix of the given string
- Print the longest prefix of the given string which is also the suffix of the same string
- Print the longest palindromic prefix of a given string
- Longest string which is prefix string of at least two strings
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Longest sub-string having frequency of each character less than equal to k
- Find the longest common prefix between two strings after performing swaps on second string
- Longest prefix which is also suffix
- Longest Common Prefix Matching | Set-6
- Longest prefix that contains same number of X and Y in an array
- Longest Common Prefix using Trie
- Find minimum shift for longest common prefix
- Minimize length of prefix of string S containing all characters of another string T
- Count subarrays where second highest lie before highest
- Sub-strings of a string that are prefix of the same string
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Longest Common Prefix using Word by Word Matching
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.