
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the longest prefix in a string that has the highest frequency across the string.
In this article, we'll discuss the problem of finding the longest prefix in a string that has the highest frequency (appears most often) across the string. We'll break it down step-by-step and show how to efficiently solve this problem.
Given a string, we need to find the longest prefix (the starting sequence) that appears most often in the string. A prefix is simply a substring that begins at the start of the string and includes one or more characters.
Let's understand with an example. For the string 'abcabc', the prefixes are:
- a
- ab
- abc
- abca
- abcab
- abcabc
Now, we need to find the longest prefix that appears most frequently. In this case:
- Prefix 'a' appears 2 times.
- Prefix 'ab' appears 2 times.
- Prefix 'abc' appears 2 times.
- Prefix 'abca' appears 1 time.
- Prefix 'abcab' appears 1 time.
- Prefix 'abcabc' appears 1 time.
The longest prefix with the highest frequency is 'abc', which appears 2 times.
Approach to Solve the Problem
To solve this problem easily, we can follow these steps:
- Find positions where the first character appears (after the first character).
- Check if the growing prefix matches at these positions.
- Increase the prefix length as long as it matches at all positions.
- Stop when a mismatch occurs and print the longest matching prefix.
Steps
Here are the steps we have taken:
- First, we identify all indices in the string where the first character appears again (starting from the second character).
- Then, we compare the growing prefix (starting from the first character) at all the identified positions to see if they match.
- Next, we gradually increase the prefix length and check if the characters at the identified positions continue to match. If they do, we increase the prefix length; if not, we stop.
- Once we can no longer extend the prefix, we print the longest matching prefix that repeats the most times.
Example
Consider the input string "abcab". First, we identify the positions where the first character ('a') appears again. Then, we compare growing prefixes like "a", "ab", "abc", and so on, at these positions. Finally, we find that the longest matching prefix is "ab" and print it as the result.
#include <bits/stdc++.h> using namespace std; // Function to find the longest prefix with the highest frequency void findLongestPrefix(string inputStr) { int prefixLength = 1, idx; int strLength = inputStr.length(); vector<int> positions; bool isFirstCharRepeated = false; // Storing all indices where the first character is found for (int i = 1; i < strLength; i++) { if (inputStr[i] == inputStr[0]) { positions.push_back(i); isFirstCharRepeated = true; } } // If the first character in the string doesn't occur again, print the whole string if (!isFirstCharRepeated) { cout << inputStr << endl; } else { int numPositions = positions.size(); // Loop until the second appearance of the first character while (prefixLength < positions[0]) { int matchCount = 0; for (idx = 0; idx < numPositions; idx++) { // Check one letter after each stored index if (inputStr[positions[idx] + prefixLength] == inputStr[prefixLength]) { matchCount++; } } // If no mismatch, move forward if (matchCount == numPositions) { prefixLength++; } // Otherwise, stop else { break; } } // Print the longest prefix for (int i = 0; i < prefixLength; i++) { cout << inputStr[i]; } cout << endl; } } // Driver Code int main() { string str = "abcab"; findLongestPrefix(str); return 0; }
The output of the code above is shown below:
ab
Time Complexity: O(n^2) The algorithm checks the string and compares prefixes at multiple positions.
Space Complexity: O(n) The algorithm uses space to store the positions where the first character appears.
Conclusion
In this article, we explain how to find the longest prefix in a string that appears most often. By looking at where the first character repeats and comparing the growing prefixes, we can easily find the longest matching one. This method offers an efficient solution to the problem.