Lexicographically smallest substring with maximum occurrences containing a’s and b’s only

Given a string s ( containing characters from ‘0’ to ‘9’) and two digits a and b. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only.
int maxFreq(string s, int a, int b)
{
    // To store frequency of digits
    int fre[10] = { 0 };
  
    // size of string
    int n = s.size();
  
    // Take lexicographically larger digit in b
    if (a > b)
        swap(a, b);
  
    // get frequency of each character
    for (int i = 0; i < n; i++)
        fre[s[i] - '0']++;
  
    // If no such string exits
    if (fre[a] == 0 and fre[b] == 0)
        return -1;
  
    // Maximum frequency
    else if (fre[a] >= fre[b])
        return a;
    else
        return b;
}
  
// Driver program
int main()
{
    int a = 4, b = 7;
  
    string s = "47744";
  
    cout << maxFreq(s, a, b);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only
  
import java.io.*;
  
class GFG {
  
  
// Function to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only.
static int maxFreq(String s, int a, int b)
{
    // To store frequency of digits
    int fre[] =  new int[10];
  
    // size of string
    int n = s.length();
  
    // Take lexicographically larger digit in b
    if (a > b)
    {
        int temp = a;
        a =b;
        b = temp;
      
    }
  
    // get frequency of each character
    for (int i = 0; i < n; i++)
        fre[s.charAt(i) - '0']++;
  
    // If no such string exits
    if (fre[a] == 0 && fre[b] == 0)
        return -1;
  
    // Maximum frequency
    else if (fre[a] >= fre[b])
        return a;
    else
        return b;
}
  
// Driver program
  
    public static void main (String[] args) {
      
    int a = 4, b = 7;
  
    String s = "47744";
  
    System.out.print(maxFreq(s, a, b));
    }
}
// This code is contributed by inder_verma

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to Find the lexicographically
# smallest substring in a given string with
# maximum frequency and contains a's and b's only
  
# Function to Find the lexicographically
# smallest substring in a given string with
# maximum frequency and contains a's and b's only.
def maxFreq(s, a, b):
    # To store frequency of digits
    fre = [0 for i in range(10)]
  
    # size of string
    n = len(s)
  
    # Take lexicographically larger digit in b
    if (a > b):
        swap(a, b)
  
    # get frequency of each character
    for i in range(0,n,1):
        a = ord(s[i]) - ord('0')
        fre[a] += 1
  
    # If no such string exits
    if (fre[a] == 0 and fre[b] == 0):
        return -1
  
    # Maximum frequency
    elif (fre[a] >= fre[b]):
        return a
    else:
        return b
  
# Driver program
if __name__ == '__main__':
    a = 4
    b = 7
  
    s = "47744"
  
    print(maxFreq(s, a, b))
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only
using System;
  
class GFG {
  
  
// Function to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only.
static int maxFreq(string s, int a, int b)
{
    // To store frequency of digits
    int []fre = new int[10];
  
    // size of string
    int n = s.Length;
  
    // Take lexicographically larger digit in b
    if (a > b)
    {
        int temp = a;
        a =b;
        b = temp;
      
    }
  
    // get frequency of each character
    for (int i = 0; i < n; i++)
        fre[s[i] - '0']++;
  
    // If no such string exits
    if (fre[a] == 0 && fre[b] == 0)
        return -1;
  
    // Maximum frequency
    else if (fre[a] >= fre[b])
        return a;
    else
        return b;
}
  
// Driver program
  
    public static void Main () {
      
    int a = 4, b = 7;
  
    string s = "47744";
  
     Console.WriteLine(maxFreq(s, a, b));
    }
}
// This code is contributed by inder_verma

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only
  
// Function to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only.
function maxFreq($s, $a, $b)
{
    // To store frequency of digits
    $fre = array_fill(0, 10, 0);
  
    // size of string
    $n = strlen($s);
  
    // Take lexicographically larger digit in b
    if ($a > $b)
    {
        $xx = $a;
        $a = $b;
        $b = $xx;}
  
     // get frequency of each character
    for ($i = 0; $i < $n; $i++)
    {
        $a = ord($s[$i]) - ord('0');
        $fre[$a] += 1;
    }
  
    // If no such string exits
    if ($fre[$a] == 0 and $fre[$b] == 0)
        return -1;
  
    // Maximum frequency
    else if ($fre[$a] >= $fre[$b])
        return $a;
    else
        return $b;
}
  
// Driver Code
$a = 4;
$b = 7;
  
$s = "47744";
  
print(maxFreq($s, $a, $b));
  
// This code is contributed by mits
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.