Open In App

Find the Substring with maximum frequency and containing only X and Y

Last Updated : 21 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S of length N consisting of numbers from(0-9) and also two numbers, one is even (say  X)and one is odd (say Y), the task is to find the substring which occurs maximum time and only contains X or Y.

Note: If two substrings have the same frequency then return the lexicographically smaller one.

Examples:

Input:  N = 5, S =”48947″, X = ‘4’, Y = ‘9’
Output:   4
Explanation: Sub-string “4” occurring maximum number of times in “48947”.

Input: N = 8, S = “22772777”, X = ‘2’, Y = ‘7’
Output: 7

 

Naive Approach:- The basic approach to solve the problem is to check for different substrings of different lengths consisting of only given even and odd numbers.

Time Complexity: O(N2)
Auxiliary Space: O(N2)

Efficient Approach: The problem can be solved efficiently with the help of the following observation:

 Just check out the occurrence of given even and odd digit.
Print out the digit from given X and Y which one occurs maximum number of times.
If both occur same number of times the print lexicographically minimum one.

Reason: The reason why we are just checking out the occurrence of the only even and odd digit and not the occurrence of the substring of length greater than 1:

Considering string: “22772777”

In this example “227” and “22” are occurring 1 time each, “27” is occurring 2 times and similarly for other substrings. But the single digit substring “7” is occurring 5 times which is the  maximum among all. 
The reason is the substrings of any length formed by only the given X and Y contain them as single length substrings. So they obviously occurs more times than the other substrings.
That is why, checking for only the given odd and even number substring of length 1 is needed.

Follow the steps to solve the problem:

  • Initializing two count variables to store the count of X and Y.
  • Traverse the string and check:
    • If the current character is even or odd and increase their respective counts.
    • Compare both the counts and return the one with a greater count.
    • If the count of both of them are same then return the lexicographically minimum one.

Below is the implementation for the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find substring
// which occur maximum number of times
char find(int N, string S, char even, char odd)
{
    // Count1 for even and count2 for odd
    // traversing the whole string
    int count1 = 0;
    int count2 = 0;
    for (int i = 0; i < N; i++) {
 
        // Checking if the character is
        // equal to even
        if (S[i] == even)
            count1++;
 
        // Checking if the character
        // is equal to odd
        else if (S[i] == odd)
            count2++;
    }
 
    // If even occur maximum times
    if (count1 > count2)
        return even;
 
    // If odd occur maximum times
    else if (count2 > count1)
        return odd;
 
    // If both occur same times
    // checking which one is
    // lexicographically minimum
    else {
        if (even - '0' < odd - '0')
            return even;
        else
            return odd;
    }
}
 
// Driver Code
int main()
{
    // Length of string
    int N = 8;
 
    // Even and odd number
    char even = '2', odd = '7';
    string S = "22772777";
 
    // Output string
    string ans;
 
    // Calling function to find string
    ans = find(N, S, even, odd);
 
    // Printing output
    cout << ans << endl;
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find substring
    // which occur maximum number of times
    public static char find(int N, String S, char even,
                            char odd)
    {
       
        // Count1 for even and count2 for odd
        // traversing the whole string
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < N; i++) {
 
            // Checking if the character is
            // equal to even
            if (S.charAt(i) == even)
                count1++;
 
            // Checking if the character
            // is equal to odd
            else if (S.charAt(i) == odd)
                count2++;
        }
 
        // If even occur maximum times
        if (count1 > count2)
            return even;
 
        // If odd occur maximum times
        else if (count2 > count1)
            return odd;
 
        // If both occur same times
        // checking which one is
        // lexicographically minimum
        else {
            if (even - '0' < odd - '0')
                return even;
            else
                return odd;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Length of string
        int N = 8;
 
        // Even and odd number
        char even = '2', odd = '7';
        String S = "22772777";
 
        // Output string
        String ans = "";
 
        // Calling function to find string
        ans += find(N, S, even, odd);
 
        // Printing output
        System.out.println(ans);
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# python3 code for the above approach
 
# Function to find substring
# which occur maximum number of times
def find(N, S, even, odd):
 
    # Count1 for even and count2 for odd
    # traversing the whole string
    count1 = 0
    count2 = 0
    for i in range(0, N):
 
        # Checking if the character is
        # equal to even
        if (S[i] == even):
            count1 += 1
 
        # Checking if the character
        # is equal to odd
        elif (S[i] == odd):
            count2 += 1
 
    # If even occur maximum times
    if (count1 > count2):
        return even
 
    # If odd occur maximum times
    elif (count2 > count1):
        return odd
 
    # If both occur same times
    # checking which one is
    # lexicographically minimum
    else:
        if (ord(even) - ord('0') < ord(odd) - ord('0')):
            return even
        else:
            return odd
 
# Driver Code
if __name__ == "__main__":
 
    # Length of string
    N = 8
 
    # Even and odd number
    even, odd = "2", '7'
    S = "22772777"
 
    # Output string
    ans = ""
 
    # Calling function to find string
    ans = find(N, S, even, odd)
 
    # Printing output
    print(ans)
 
# This code is contributed by rakeshsahni


C#




// C# code for the above approach
using System;
class GFG {
 
  // Function to find substring
  // which occur maximum number of times
  static char find(int N, string S, char even, char odd)
  {
 
    // Count1 for even and count2 for odd
    // traversing the whole string
    int count1 = 0;
    int count2 = 0;
    for (int i = 0; i < N; i++) {
 
      // Checking if the character is
      // equal to even
      if (S[i] == even)
        count1++;
 
      // Checking if the character
      // is equal to odd
      else if (S[i] == odd)
        count2++;
    }
 
    // If even occur maximum times
    if (count1 > count2)
      return even;
 
    // If odd occur maximum times
    else if (count2 > count1)
      return odd;
 
    // If both occur same times
    // checking which one is
    // lexicographically minimum
    else {
      if (even - '0' < odd - '0')
        return even;
      else
        return odd;
    }
  }
 
  // Driver code
  public static int Main()
  {
    // Length of string
    int N = 8;
 
    // Even and odd number
    char even = '2', odd = '7';
    string S = "22772777";
 
    // Output string
    string ans = "";
 
    // Calling function to find string
    ans += find(N, S, even, odd);
 
    // Printing output
    Console.WriteLine(ans);
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript




<script>
// JavaScript code for the above approach
 
// Function to find substring
// which occur maximum number of times
function find(N, S, even, odd)
{
    // Count1 for even and count2 for odd
    // traversing the whole string
    var count1 = 0;
    var count2 = 0;
    for (var i = 0; i < N; i++) {
 
        // Checking if the character is
        // equal to even
        if (S[i] == even)
            count1++;
 
        // Checking if the character
        // is equal to odd
        else if (S[i] == odd)
            count2++;
    }
 
    // If even occur maximum times
    if (count1 > count2)
        return even;
 
    // If odd occur maximum times
    else if (count2 > count1)
        return odd;
 
    // If both occur same times
    // checking which one is
    // lexicographically minimum
    else {
        if (even - '0' < odd - '0')
            return even;
        else
            return odd;
    }
}
 
// Driver Code
 
// Length of string
var N = 8;
 
// Even and odd number
var even = '2';
var odd = '7';
var S = "22772777";
 
// Output string
var ans;
 
// Calling function to find string
ans = find(N, S, even, odd);
 
// Printing output
document.write(ans);
 
// This code is contributed by phasing17
</script>


Output

7

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads