给定一个字符串S和一个整数N ,任务是检查是否可以从给定字符串的字符生成任何字符串N次。如果可能,请打印Yes 。否则,打印No 。
例子:
Input: S = “caacbb”, N = 2
Output: Yes
Explanation: All possible strings that can be generated N(= 2) times are {“abc”, “ab”, “aa”, “bb”, “cc”, “bc”, “ca”}
Input: S = “cbacbac”, N = 3
Output: No
Explanation: Since none of the characters occurs N times, therefore no string can be generated N times from the characters of the given string.
朴素方法:解决问题的最简单方法是生成给定字符串所有可能长度的所有可能排列,并检查是否有任何排列出现N次。如果发现是真的,打印“是” 。否则,打印“否”
时间复杂度: O(N*L!),其中 L 是给定字符串的长度。
辅助空间: O(L)
高效的方法:想法是存储所有字符的频率,并计算至少出现N次的字符数。请按照以下步骤解决问题:
- 将字符串中每个字符的频率存储在一个数组中,比如freq[] 。
- 遍历freq[]数组,对于每个第i个字符,检查freq[i] >= N是否。
- 如果发现任何字符满足上述条件,则打印Yes 。否则,打印No 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the freq of
// any character is divisible by N
bool isSame(string str, int n)
{
// Stores the frequency of characters
map mp;
for (int i = 0; i < str.length(); i++) {
mp[str[i] - 'a']++;
}
for (auto it : mp) {
// If frequency of a character
// is not divisible by n
if ((it.second) >= n) {
return true;
}
}
// If no character has frequency
// at least N
return false;
}
// Driver Code
int main()
{
string str = "ccabcba";
int n = 4;
// Function Call
if (isSame(str, n)) {
cout << "Yes";
}
else {
cout << "No";
}
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the freq of
// any character is divisible by N
static boolean isSame(String str, int n)
{
// Stores the frequency of characters
HashMap mp = new HashMap();
for (int i = 0; i < str.length(); i++)
{
if(mp.containsKey(str.charAt(i) - 'a'))
{
mp.put(str.charAt(i) - 'a',
mp.get(str.charAt(i) - 'a') + 1);
}
else
{
mp.put(str.charAt(i) - 'a', 1);
}
}
for (Map.Entry it : mp.entrySet())
{
// If frequency of a character
// is not divisible by n
if ((it.getValue()) >= n)
{
return true;
}
}
// If no character has frequency
// at least N
return false;
}
// Driver Code
public static void main(String[] args)
{
String str = "ccabcba";
int n = 4;
// Function Call
if (isSame(str, n))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
from collections import defaultdict
# Function to check if the freq of
# any character is divisible by N
def isSame(str, n):
# Stores the frequency of characters
mp = defaultdict(lambda : 0)
for i in range(len(str)):
mp[ord(str[i]) - ord('a')] += 1
for it in mp.keys():
# If frequency of a character
# is not divisible by n
if(mp[it] >= n):
return True
# If no character has frequency
# at least N
return False
# Driver Code
str = "ccabcba"
n = 4
# Function call
if(isSame(str, n)):
print("Yes")
else:
print("No")
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if the freq of
// any character is divisible by N
static bool isSame(String str, int n)
{
// Stores the frequency of characters
Dictionary mp = new Dictionary();
for (int i = 0; i < str.Length; i++)
{
if(mp.ContainsKey(str[i] - 'a'))
{
mp[str[i] - 'a'] =
mp[str[i] - 'a'] + 1;
}
else
{
mp.Add(str[i] - 'a', 1);
}
}
foreach (KeyValuePair it in mp)
{
// If frequency of a character
// is not divisible by n
if ((it.Value) >= n)
{
return true;
}
}
// If no character has frequency
// at least N
return false;
}
// Driver Code
public static void Main(String[] args)
{
String str = "ccabcba";
int n = 4;
// Function Call
if (isSame(str, n))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
No
时间复杂度: O(L),其中 L 是给定字符串的长度
辅助空间: O(L)