Largest index for each distinct character in given string with frequency K
Last Updated :
29 Dec, 2022
Given a string S consisting of lowercase English letters and an integer K, the task is to find, for each distinct character in S, the largest index having this character exactly K times. If no such characters exist, print -1. Print the result in a lexicographical ordering.
Note: Consider 0-based indexing in S.
Examples:
Input: S = “cbaabaacbcd”, K = 2
Output: { {a 4}, {b 7}, {c 8} }
Explanation:
For ‘a’, the largest index having 2 a’s is “cbaab”.
For ‘b’, the largest index having 2 b’s is “cbaabaac”.
For ‘c’, the largest index having 2 c’s is “cbaabaacb”.
For ‘d’, the is no index up to which we have 2 d’s
Input: P = “acbacbacbaba”, K = 3
Output: { {a 8}, {b 9}, {c 11} }
Approach: The idea is to first find all the distinct characters in string S. Then for each lowercase English character, check whether it is present in S or not and run a for loop from the beginning of S and maintain the count of that character that occurred till now. When the count becomes equal to K update the index answer accordingly. Finally, append this character and its corresponding index in the vector result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void maxSubstring(string& S, int K, int N)
{
int freq[26];
memset (freq, 0, sizeof freq);
for ( int i = 0; i < N; ++i) {
freq[S[i] - 'a' ] = 1;
}
vector<pair< char , int > > answer;
for ( int i = 0; i < 26; ++i) {
if (freq[i] == 0)
continue ;
char ch = i + 97;
int count = 0;
int index = -1;
for ( int j = 0; j < N; ++j) {
if (S[j] == ch)
count++;
if (count == K)
index = j;
}
answer.push_back({ ch, index });
}
int flag = 0;
for ( int i = 0; i < ( int )answer.size(); ++i) {
if (answer[i].second > -1) {
flag = 1;
cout << answer[i].first << " "
<< answer[i].second << endl;
}
}
if (flag == 0)
cout << "-1" << endl;
}
int main()
{
string S = "cbaabaacbcd" ;
int K = 2;
int N = S.length();
maxSubstring(S, K, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{ char first;
int second;
public pair( char first, int second)
{
this .first = first;
this .second = second;
}
}
static void maxSubString( char [] S,
int K, int N)
{
int []freq = new int [ 26 ];
for ( int i = 0 ; i < N; ++i)
{
freq[S[i] - 'a' ] = 1 ;
}
Vector<pair> answer = new Vector<pair>();
for ( int i = 0 ; i < 26 ; ++i)
{
if (freq[i] == 0 )
continue ;
char ch = ( char ) (i + 97 );
int count = 0 ;
int index = - 1 ;
for ( int j = 0 ; j < N; ++j)
{
if (S[j] == ch)
count++;
if (count == K)
index = j;
}
answer.add( new pair(ch, index ));
}
int flag = 0 ;
for ( int i = 0 ; i < ( int )answer.size(); ++i)
{
if (answer.get(i).second > - 1 )
{
flag = 1 ;
System.out.print(answer.get(i).first + " " +
answer.get(i).second + "\n" );
}
}
if (flag == 0 )
System.out.print( "-1" + "\n" );
}
public static void main(String[] args)
{
String S = "cbaabaacbcd" ;
int K = 2 ;
int N = S.length();
maxSubString(S.toCharArray(), K, N);
}
}
|
Python3
def maxSubstring(S, K, N):
freq = [ 0 for i in range ( 26 )]
for i in range (N):
freq[ ord (S[i]) - 97 ] = 1
answer = []
for i in range ( 26 ):
if (freq[i] = = 0 ):
continue
ch = chr (i + 97 )
count = 0
index = - 1
for j in range (N):
if (S[j] = = ch):
count + = 1
if (count = = K):
index = j
answer.append([ch, index])
flag = 0
for i in range ( len (answer)):
if (answer[i][ 1 ] > - 1 ):
flag = 1
print (answer[i][ 0 ],
answer[i][ 1 ])
if (flag = = 0 ):
print ( "-1" )
if __name__ = = '__main__' :
S = "cbaabaacbcd"
K = 2
N = len (S)
maxSubstring(S, K, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class pair
{
public char first;
public int second;
public pair( char first, int second)
{
this .first = first;
this .second = second;
}
}
static void maxSubString( char [] S,
int K, int N)
{
int []freq = new int [26];
for ( int i = 0; i < N; ++i)
{
freq[S[i] - 'a' ] = 1;
}
List<pair> answer = new List<pair>();
for ( int i = 0; i < 26; ++i)
{
if (freq[i] == 0)
continue ;
char ch = ( char )(i + 97);
int count = 0;
int index = -1;
for ( int j = 0; j < N; ++j)
{
if (S[j] == ch)
count++;
if (count == K)
index = j;
}
answer.Add( new pair(ch, index));
}
int flag = 0;
for ( int i = 0; i < ( int )answer.Count; ++i)
{
if (answer[i].second > -1)
{
flag = 1;
Console.Write(answer[i].first + " " +
answer[i].second + "\n" );
}
}
if (flag == 0)
Console.Write( "-1" + "\n" );
}
public static void Main(String[] args)
{
String S = "cbaabaacbcd" ;
int K = 2;
int N = S.Length;
maxSubString(S.ToCharArray(), K, N);
}
}
|
Javascript
<script>
function maxSubString(S, K, N) {
var freq = new Array(26).fill(0);
for ( var i = 0; i < N; ++i) {
freq[S[i].charCodeAt(0) - "a" .charCodeAt(0)] = 1;
}
var answer = [];
for ( var i = 0; i < 26; ++i) {
if (freq[i] === 0)
continue ;
var ch = String.fromCharCode(i + 97);
var count = 0;
var index = -1;
for ( var j = 0; j < N; ++j) {
if (S[j] === ch) count++;
if (count === K) index = j;
}
answer.push([ch, index]);
}
var flag = 0;
for ( var i = 0; i < answer.length; ++i) {
if (answer[i][1] > -1) {
flag = 1;
document.write(answer[i][0] + " " + answer[i][1] + "<br>" );
}
}
if (flag === 0)
document.write( "-1" + "<br>" );
}
var S = "cbaabaacbcd" ;
var K = 2;
var N = S.length;
maxSubString(S.split( "" ), K, N);
</script>
|
Time Complexity: O(26 * N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...