Given a string, str, the task is to find the indices of the given string such that the count of lexicographically smaller characters on the left and right sides of that index is equal and non-zero.
Examples:
Input: str = “aabacdabbb”
Output: 2 4
Explanation:
Count of smaller characters on the left side of index 2 is 2 and right side of index 2 is also 2.
Count of smaller characters on the left side of index 4 is 4 and right side of index 4 is also 4.
Therefore, the required output is 2 4.Input: “geeksforgeeks”
Output: 5
Explanation:
Count of smaller characters on the left side of index 5 is 2 and right side of index 5 is also 2.
Therefore, the required output is 5.
Naive approach: The simplest approach to solve this problem is to traverse the given string and count the number of lexicographically smaller characters on the left side and right side of each index of the given string. For each index, check if the count of lexicographically smaller characters on the left side and right side of the current index are equal or not. If found to be true then print the current index.
Time Complexity: (N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is to use Hashing. Follow the steps below to solve the problem:
- Initialize an array, say cntFre[] to store the frequency of each character of the given string
- Traverse the given string and store the frequency of each character of the given string.
- Initialize an array, say cntLeftFreq[] to store the frequency of all the characters present on the left side of the current index of the given string.
- Traverse the given string and store the frequency of all the lexicographically smaller characters present on the left side of the current index.
- For each index, check if count of lexicographically smaller characters on the left side and the right side of current index is equal or not. If found to be true then print the current index of the given string.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find indexes // of the given string that // satisfy the condition void printIndexes(string str) { // Stores length of // given string int N = str.length(); // Stores frequncy of // each character of str int cntFreq[256] = {0}; for ( int i = 0; i < N; i++) { // Update frequency of // current character cntFreq[str[i]]++; } // cntLeftFreq[i] Stores frequncy // of characters present on // the left side of index i. int cntLeftFreq[256] = {0}; // Traverse the given string for ( int i = 0; i < N; i++) { // Stores count of smaller // characters on left side of i. int cntLeft = 0; // Stores count of smaller // characters on Right side of i. int cntRight = 0; // Traverse smaller characters // on left side of index i. for ( int j = str[i] - 1; j >= 0; j--) { // Update cntLeft cntLeft += cntLeftFreq[j]; // Update cntRight cntRight += cntFreq[j] - cntLeftFreq[j]; } // Update cntLeftFreq[str[i]] cntLeftFreq[str[i]]++; // If count of smaller elements // on both sides equal if (cntLeft == cntRight && cntLeft != 0) { // Print current index; cout<<i<< " " ; } } } // Driver Code int main() { string str = "aabacdabbb" ; printIndexes(str); } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to find indexes // of the given String that // satisfy the condition static void printIndexes( char [] str) { // Stores length of // given String int N = str.length; // Stores frequncy of // each character of str int []cntFreq = new int [ 256 ]; for ( int i = 0 ; i < N; i++) { // Update frequency of // current character cntFreq[str[i]]++; } // cntLeftFreq[i] Stores frequncy // of characters present on // the left side of index i. int []cntLeftFreq = new int [ 256 ]; // Traverse the given String for ( int i = 0 ; i < N; i++) { // Stores count of smaller // characters on left side of i. int cntLeft = 0 ; // Stores count of smaller // characters on Right side of i. int cntRight = 0 ; // Traverse smaller characters // on left side of index i. for ( int j = str[i] - 1 ; j >= 0 ; j--) { // Update cntLeft cntLeft += cntLeftFreq[j]; // Update cntRight cntRight += cntFreq[j] - cntLeftFreq[j]; } // Update cntLeftFreq[str[i]] cntLeftFreq[str[i]]++; // If count of smaller elements // on both sides equal if (cntLeft == cntRight && cntLeft != 0 ) { // Print current index; System.out.print(i + " " ); } } } // Driver Code public static void main(String[] args) { String str = "aabacdabbb" ; printIndexes(str.toCharArray()); } } // This code is contributed by gauravrajput1 |
Python3
# Python3 program to implement # the above approach # Function to find indexes # of the given that # satisfy the condition def printIndexes(strr): # Stores length of # given string N = len (strr) # Stores frequncy of # each character of strr cntFreq = [ 0 ] * 256 for i in range (N): # Update frequency of # current character cntFreq[ ord (strr[i])] + = 1 # cntLeftFreq[i] Stores frequncy # of characters present on # the left side of index i. cntLeftFreq = [ 0 ] * 256 # Traverse the given strring for i in range (N): # Stores count of smaller # characters on left side of i. cntLeft = 0 # Stores count of smaller # characters on Right side of i. cntRight = 0 # Traverse smaller characters # on left side of index i. for j in range ( ord (strr[i]) - 1 , - 1 , - 1 ): # Update cntLeft cntLeft + = cntLeftFreq[j] # Update cntRight cntRight + = (cntFreq[j] - cntLeftFreq[j]) # Update cntLeftFreq[strr[i]] cntLeftFreq[ ord (strr[i])] + = 1 # If count of smaller elements # on both sides equal if (cntLeft = = cntRight and cntLeft ! = 0 ): # Print current index print (i, end = " " ) # Driver Code if __name__ = = '__main__' : strr = "aabacdabbb" printIndexes(strr) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find indexes // of the given string that // satisfy the condition static void printIndexes( char [] str) { // Stores length of // given string int N = str.Length; // Stores frequncy of // each character of str int []cntFreq = new int [256]; for ( int i = 0; i < N; i++) { // Update frequency of // current character cntFreq[str[i]]++; } // cntLeftFreq[i] Stores frequncy // of characters present on // the left side of index i. int []cntLeftFreq = new int [256]; // Traverse the given string for ( int i = 0; i < N; i++) { // Stores count of smaller // characters on left side of i. int cntLeft = 0; // Stores count of smaller // characters on Right side of i. int cntRight = 0; // Traverse smaller characters // on left side of index i. for ( int j = str[i] - 1; j >= 0; j--) { // Update cntLeft cntLeft += cntLeftFreq[j]; // Update cntRight cntRight += cntFreq[j] - cntLeftFreq[j]; } // Update cntLeftFreq[str[i]] cntLeftFreq[str[i]]++; // If count of smaller elements // on both sides equal if (cntLeft == cntRight && cntLeft != 0) { // Print current index; Console.Write(i + " " ); } } } // Driver Code public static void Main() { string str = "aabacdabbb" ; printIndexes(str.ToCharArray()); } } // This code is contributed by SURENDRA_GANGWAR |
2 4
Time Complexity: O(N * 256)
Auxiliary Space: O(256)
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.