给定两个字符串str1 和 str2,任务是打印使用str1 的字符可以形成str2的次数。然而,在STR1的任何索引的字符只能使用一次在STR2的形成中使用。
例子:
Input: str1 = “arajjhupoot”, str2 = “rajput”
Output: 1
Explanation:
str2 can only be formed once using characters of str1.
Input: str1 = “foreeksgekseg”, str2 = “geeks”
Output: 2
方法:既然问题对使用str1中的字符只有一次形成STR2限制。如果一个字符已用于形成一个 str2,则它不能用于形成另一个 str2。 str2 的每个字符都必须存在于 str1 中,至少要形成一个 str1。如果STR2的所有字符都在STR1已经存在,那么它在STR1最小发生的字符将可使用一次STR1的字符形成STR2的数量。以下是步骤:
- 创建一个哈希数组,用于存储 str 中每个字符的出现次数。
- 迭代 str2 的所有字符,并找到 str1 中每个字符出现次数最少的一次。
- 返回将作为答案的最小出现次数。
下面是上述方法的实现:
C++
/// C++ program to print the number of times
// str2 can be formed from str1 using the
// characters of str1 only once
#include
using namespace std;
// Function to find the number of str2
// that can be formed using characters of str1
int findNumberOfTimes(string str1, string str2)
{
int freq[26] = { 0 };
int freq2[26] = { 0 };
int l1 = str1.length();
int l2 = str2.length();
// iterate and mark the frequencies of
// all characters in str1
for (int i = 0; i < l1; i++)
freq[str1[i] - 'a'] += 1;
for (int i = 0; i < l2; i++)
freq2[str2[i] - 'a'] += 1;
int count = INT_MAX;
// find the minimum frequency of
// every character in str1
for (int i = 0; i < l2; i++)
{
if(freq2[str2[i]-'a']!=0)
count = min(count, freq[str2[i] - 'a']/freq2[str2[i]-'a']);
}
return count;
}
// Driver Code
int main()
{
string str1 = "foreeksgekseg";
string str2 = "geeks";
cout << findNumberOfTimes(str1, str2)
<< endl;
return 0;
}
Java
// Java program to print the number of times
// str2 can be formed from str1 using the
// characters of str1 only once
class GFG {
// Function to find the number of str2
// that can be formed using characters of str1
static int findNumberOfTimes(String str1, String str2)
{
int freq[] = new int[26];
int freq2[] = new int[26];
int l1 = str1.length();
// iterate and mark the frequencies of
// all characters in str1
for (int i = 0; i < l1; i++)
{
freq[str1.charAt(i) - 'a'] += 1;
}
int l2 = str2.length();
for (int i = 0; i < l2; i++)
{
freq2[str2.charAt(i) - 'a'] += 1;
}
int count = Integer.MAX_VALUE;
// find the minimum frequency of
// every character in str1
for (int i = 0; i < l2; i++)
{
if(freq2[str2.charAt(i)-'a']!=0)
count = Math.min(count,
freq[str2.charAt(i) - 'a']/freq2[str2.charAt(i)-'a']);
}
return count;
}
public static void main(String[] args) {
String str1 = "foreeksgekseg";
String str2 ="geeks";
System.out.println(findNumberOfTimes(str1, str2));
}
}
/* This code is contributed by 29AjayKumar*/
Python3
# Python3 program to print the number of
# times str2 can be formed from str1 using
# the characters of str1 only once
import sys
# Function to find the number of str2
# that can be formed using characters of str1
def findNumberOfTimes(str1, str2):
freq = [0] * 26
l1 = len(str1)
freq2= [0] * 26
l2 = len(str2)
# iterate and mark the frequencies
# of all characters in str1
for i in range(l1):
freq[ord(str1[i]) - ord("a")] += 1
for i in range(l2):
freq2[ord(str2[i]) - ord("a")] += 1
count = sys.maxsize
# find the minimum frequency of
# every character in str1
for i in range(l2):
count = min(count, freq[ord(str2[i])
- ord('a')]/freq2[ord(str2[i])-ord('a')])
return count
# Driver Code
if __name__ == '__main__':
str1 = "foreeksgekseg"
str2 = "geeks"
print(findNumberOfTimes(str1, str2))
# This code is contributed by PrinciRaj1992
C#
// C# program to print the number of
// times str2 can be formed from str1
// using the characters of str1 only once
using System;
class GFG {
// Function to find the number of
// str2 that can be formed using
// characters of str1
static int findNumberOfTimes(String str1, String str2)
{
int[] freq = new int[26];
int l1 = str1.Length;
int[] freq2 = new int[26];
int l2 = str2.Length;
// iterate and mark the frequencies
// of all characters in str1
for (int i = 0; i < l1; i++)
{
freq[str1[i] - 'a'] += 1;
}
for (int i = 0; i < l2; i++)
{
freq2[str2[i] - 'a'] += 1;
}
int count = int.MaxValue;
// find the minimum frequency of
// every character in str1
for (int i = 0; i < l2; i++)
{
if (freq2[str2[i] - 'a'] != 0)
count = Math.Min(
count, freq[str2[i] - 'a']
/ freq2[str2[i] - 'a']);
}
return count;
}
// Driver Code
public static void Main()
{
String str1 = "foreeksgekseg";
String str2 = "geeks";
Console.Write(findNumberOfTimes(str1, str2));
}
}
// This code is contributed by 29AjayKumar
PHP
Javascript
输出
2
时间复杂度: O(max(l1,l2)),其中 l1 和 l2 分别是 str1 和 str2 的长度。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。