Lexicographically smallest substring with maximum occurrences containing a’s and b’s only
Last Updated :
09 Sep, 2022
Given a string ( containing characters from ‘0’ to ‘9’) and two digits and . The task is to find the substring in the given string with maximum occurrences and containing a’s and b’s only. If there are two or more such substrings with same frequencies then print the lexicographically smallest. If there does not exists any such substring then print -1.
Examples:
Input : str = "47", a = 4, b = 7
Output : 4
Input : str = "23", a = 4, b = 7
Output : -1
The idea is to observe that we need to find the substring with maximum number of occurrences. So, if we consider substrings that contains both a’s and b’s then the number of occurrences will be less than if we consider the substrings with single digits ‘a’ and ‘b’ individually.
So, the idea is to calculate the frequency of digits of ‘a’ and ‘b’ in the string and the one with maximum frequency will be the answer.
Note: If both digits have same frequency then the digit which is lexicographically smaller among ‘a’ and ‘b’ will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxFreq(string s, int a, int b)
{
int fre[10] = { 0 };
int n = s.size();
if (a > b)
swap(a, b);
for ( int i = 0; i < n; i++)
fre[s[i] - '0' ]++;
if (fre[a] == 0 and fre[b] == 0)
return -1;
else if (fre[a] >= fre[b])
return a;
else
return b;
}
int main()
{
int a = 4, b = 7;
string s = "47744" ;
cout << maxFreq(s, a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxFreq(String s, int a, int b)
{
int fre[] = new int [ 10 ];
int n = s.length();
if (a > b)
{
int temp = a;
a =b;
b = temp;
}
for ( int i = 0 ; i < n; i++)
fre[s.charAt(i) - '0' ]++;
if (fre[a] == 0 && fre[b] == 0 )
return - 1 ;
else if (fre[a] >= fre[b])
return a;
else
return b;
}
public static void main (String[] args) {
int a = 4 , b = 7 ;
String s = "47744" ;
System.out.print(maxFreq(s, a, b));
}
}
|
Python3
def maxFreq(s, a, b):
fre = [ 0 for i in range ( 10 )]
n = len (s)
if (a > b):
swap(a, b)
for i in range ( 0 ,n, 1 ):
a = ord (s[i]) - ord ( '0' )
fre[a] + = 1
if (fre[a] = = 0 and fre[b] = = 0 ):
return - 1
elif (fre[a] > = fre[b]):
return a
else :
return b
if __name__ = = '__main__' :
a = 4
b = 7
s = "47744"
print (maxFreq(s, a, b))
|
C#
using System;
class GFG {
static int maxFreq( string s, int a, int b)
{
int []fre = new int [10];
int n = s.Length;
if (a > b)
{
int temp = a;
a =b;
b = temp;
}
for ( int i = 0; i < n; i++)
fre[s[i] - '0' ]++;
if (fre[a] == 0 && fre[b] == 0)
return -1;
else if (fre[a] >= fre[b])
return a;
else
return b;
}
public static void Main () {
int a = 4, b = 7;
string s = "47744" ;
Console.WriteLine(maxFreq(s, a, b));
}
}
|
PHP
<?php
function maxFreq( $s , $a , $b )
{
$fre = array_fill (0, 10, 0);
$n = strlen ( $s );
if ( $a > $b )
{
$xx = $a ;
$a = $b ;
$b = $xx ;}
for ( $i = 0; $i < $n ; $i ++)
{
$a = ord( $s [ $i ]) - ord( '0' );
$fre [ $a ] += 1;
}
if ( $fre [ $a ] == 0 and $fre [ $b ] == 0)
return -1;
else if ( $fre [ $a ] >= $fre [ $b ])
return $a ;
else
return $b ;
}
$a = 4;
$b = 7;
$s = "47744" ;
print (maxFreq( $s , $a , $b ));
?>
|
Javascript
<script>
function maxFreq(s, a, b)
{
var fre = new Array(10).fill(0);
var n = s.length;
if (a > b) {
var temp = a;
a = b;
b = temp;
}
for ( var i = 0; i < n; i++)
fre[s[i].charCodeAt(0) - "0" .charCodeAt(0)]++;
if (fre[a] === 0 && fre[b] === 0) return -1;
else if (fre[a] >= fre[b]) return a;
else return b;
}
var a = 4,
b = 7;
var s = "47744" ;
document.write(maxFreq(s, a, b));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n is the length of the string s.
- Auxiliary Space: O(10)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...