📌  相关文章
📜  词典上最小的子字符串,最大出现次数仅包含a和b

📅  最后修改于: 2021-04-25 00:28:33             🧑  作者: Mango

给定一个字符串s (含有从“0”字符,“9”)和两个数字ab 。任务是在给定的字符串找到出现次数最多且仅包含a和b的子字符串。如果有两个或更多个具有相同频率的子字符串,则按字典顺序最小打印。如果不存在任何这样的子字符串,则打印-1。

例子

Input : str = "47", a = 4, b = 7 
Output : 4

Input : str = "23", a = 4, b = 7
Output : -1

这样做的目的是观察到我们需要找到出现次数最多的子字符串。因此,如果我们考虑同时包含a和b的子字符串,那么出现的次数将少于我们单独考虑具有单个数字“ a”和“ b”的子字符串的情况。

所以,这个想法是计算的字符串中的“A”和“B”,并与最高频率的人会是答案数字出现的频率。

注意:如果两个数字具有相同的频率,那么在字典上’a’和’b’中较小的数字将是答案。

下面是上述方法的实现:

C++
// CPP program to Find the lexicographically
// smallest substring in a given string with
// maximum frequency and contains a's and b's only
  
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


PHP
 $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
?>


输出:
4