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 is 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 the 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++
#include <bits/stdc++.h>
using namespace std;
void printIndexes(string str)
{
int N = str.length();
int cntFreq[256] = {0};
for ( int i = 0; i < N;
i++) {
cntFreq[str[i]]++;
}
int cntLeftFreq[256] = {0};
for ( int i = 0; i < N; i++) {
int cntLeft = 0;
int cntRight = 0;
for ( int j = str[i] - 1;
j >= 0; j--) {
cntLeft += cntLeftFreq[j];
cntRight += cntFreq[j]
- cntLeftFreq[j];
}
cntLeftFreq[str[i]]++;
if (cntLeft == cntRight &&
cntLeft != 0) {
cout<<i<< " " ;
}
}
}
int main()
{
string str = "aabacdabbb" ;
printIndexes(str);
}
|
Java
import java.util.*;
class GFG{
static void printIndexes( char [] str)
{
int N = str.length;
int []cntFreq = new int [ 256 ];
for ( int i = 0 ; i < N; i++)
{
cntFreq[str[i]]++;
}
int []cntLeftFreq = new int [ 256 ];
for ( int i = 0 ; i < N; i++)
{
int cntLeft = 0 ;
int cntRight = 0 ;
for ( int j = str[i] - 1 ;
j >= 0 ; j--)
{
cntLeft += cntLeftFreq[j];
cntRight += cntFreq[j] -
cntLeftFreq[j];
}
cntLeftFreq[str[i]]++;
if (cntLeft == cntRight &&
cntLeft != 0 )
{
System.out.print(i + " " );
}
}
}
public static void main(String[] args)
{
String str = "aabacdabbb" ;
printIndexes(str.toCharArray());
}
}
|
Python3
def printIndexes(strr):
N = len (strr)
cntFreq = [ 0 ] * 256
for i in range (N):
cntFreq[ ord (strr[i])] + = 1
cntLeftFreq = [ 0 ] * 256
for i in range (N):
cntLeft = 0
cntRight = 0
for j in range ( ord (strr[i]) - 1 , - 1 , - 1 ):
cntLeft + = cntLeftFreq[j]
cntRight + = (cntFreq[j] -
cntLeftFreq[j])
cntLeftFreq[ ord (strr[i])] + = 1
if (cntLeft = = cntRight and cntLeft ! = 0 ):
print (i, end = " " )
if __name__ = = '__main__' :
strr = "aabacdabbb"
printIndexes(strr)
|
C#
using System;
class GFG{
static void printIndexes( char [] str)
{
int N = str.Length;
int []cntFreq = new int [256];
for ( int i = 0; i < N; i++)
{
cntFreq[str[i]]++;
}
int []cntLeftFreq = new int [256];
for ( int i = 0; i < N; i++)
{
int cntLeft = 0;
int cntRight = 0;
for ( int j = str[i] - 1;
j >= 0; j--)
{
cntLeft += cntLeftFreq[j];
cntRight += cntFreq[j] -
cntLeftFreq[j];
}
cntLeftFreq[str[i]]++;
if (cntLeft == cntRight &&
cntLeft != 0)
{
Console.Write(i + " " );
}
}
}
public static void Main()
{
string str = "aabacdabbb" ;
printIndexes(str.ToCharArray());
}
}
|
Javascript
<script>
function printIndexes(str)
{
var N = str.length;
var cntFreq = Array(256).fill(0);
for ( var i = 0; i < N;
i++) {
cntFreq[str[i].charCodeAt(0)]++;
}
var cntLeftFreq = Array(256).fill(0);
for ( var i = 0; i < N; i++) {
var cntLeft = 0;
var cntRight = 0;
for ( var j = str[i].charCodeAt(0) - 1;
j >= 0; j--) {
cntLeft += cntLeftFreq[j];
cntRight += cntFreq[j]
- cntLeftFreq[j];
}
cntLeftFreq[str[i].charCodeAt(0)]++;
if (cntLeft == cntRight &&
cntLeft != 0) {
document.write( i + " " );
}
}
}
var str = "aabacdabbb" ;
printIndexes(str);
</script>
|
Time Complexity: O(N * 256)
Auxiliary Space: O(256)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Jul, 2021
Like Article
Save Article