给定两个字符串,任务是检查一个字符(对于每个字符)在一个字符串中的频率是多倍还是另一个字符串的一个因子。如果是,则输出“YES”,否则输出“NO”。
例子:
Input: s1 = “aabccd”, s2 = “bbbaaaacc”
Output: YES
Frequency of ‘a’ in s1 and s2 are 2 and 4 respectively, and 2 is a factor of 4
Frequency of ‘b’ in s1 and s2 are 1 and 3 respectively, and 1 is a factor of 3
Frequency of ‘c’ in s1 and s2 are same hence it also satisfies.
Frequency of ‘d’ in s1 and s2 are 1 and 0 respectively, but 0 is a multiple of every number, hence satisfied.
Hence, the answer YES.
Input: s1 = “hhdwjwqq”, s2 = “qwjdddhhh”
Output: NO
方法:
- 将 s1 中字符的频率存储在第一个映射 STL 中。
- 将 s2 中字符的频率存储在第二个映射 STL 中。
- 设字符在第一张地图中出现的频率为 F1。我们还假设这个字符在第二张地图中出现的频率是 F2。
- 检查 F1%F2 和 F2%F1(模运算)。如果其中一个为 0,则满足条件。
- 检查所有字符。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function that checks if the frequency of character
// are a factor or multiple of each other
bool multipleOrFactor(string s1, string s2)
{
// map store frequency of each character
map m1, m2;
for (int i = 0; i < s1.length(); i++)
m1[s1[i]]++;
for (int i = 0; i < s2.length(); i++)
m2[s2[i]]++;
map::iterator it;
for (it = m1.begin(); it != m1.end(); it++) {
// if any frequency is 0, then continue
// as condition is satisfied
if (m2.find((*it).first) == m2.end())
continue;
// if factor or multiple, then condition satified
if (m2[(*it).first] % (*it).second == 0
|| (*it).second % m2[(*it).first] == 0)
continue;
// if condition not satisfied
else
return false;
}
}
// Driver code
int main()
{
string s1 = "geeksforgeeks";
string s2 = "geeks";
multipleOrFactor(s1, s2) ? cout << "YES"
: cout << "NO";
return 0;
}
Java
// Java implementation of above approach
import java.util.HashMap;
class GFG
{
// Function that checks if the frequency of character
// are a factor or multiple of each other
public static boolean multipleOrFactor(String s1, String s2)
{
// map store frequency of each character
HashMap m1 = new HashMap<>();
HashMap m2 = new HashMap<>();
for (int i = 0; i < s1.length(); i++)
{
if (m1.containsKey(s1.charAt(i)))
{
int x = m1.get(s1.charAt(i));
m1.put(s1.charAt(i), ++x);
}
else
m1.put(s1.charAt(i), 1);
}
for (int i = 0; i < s2.length(); i++)
{
if (m2.containsKey(s2.charAt(i)))
{
int x = m2.get(s2.charAt(i));
m2.put(s2.charAt(i), ++x);
}
else
m2.put(s2.charAt(i), 1);
}
for (HashMap.Entry entry : m1.entrySet())
{
// if any frequency is 0, then continue
// as condition is satisfied
if (!m2.containsKey(entry.getKey()))
continue;
// if factor or multiple, then condition satified
if (m2.get(entry.getKey()) != null &&
(m2.get(entry.getKey()) % entry.getValue() == 0
|| entry.getValue() % m2.get(entry.getKey()) == 0))
continue;
// if condition not satisfied
else
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
String s1 = "geeksforgeeks", s2 = "geeks";
if (multipleOrFactor(s1, s2))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of above approach
from collections import defaultdict
# Function that checks if the frequency of
# character are a factor or multiple of each other
def multipleOrFactor(s1, s2):
# map store frequency of each character
m1 = defaultdict(lambda:0)
m2 = defaultdict(lambda:0)
for i in range(0, len(s1)):
m1[s1[i]] += 1
for i in range(0, len(s2)):
m2[s2[i]] += 1
for it in m1:
# if any frequency is 0, then continue
# as condition is satisfied
if it not in m2:
continue
# if factor or multiple, then condition satified
if (m2[it] % m1[it] == 0 or
m1[it] % m2[it] == 0):
continue
# if condition not satisfied
else:
return False
return True
# Driver code
if __name__ == "__main__":
s1 = "geeksforgeeks"
s2 = "geeks"
if multipleOrFactor(s1, s2): print("YES")
else: print("NO")
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function that checks if the
// frequency of character are
// a factor or multiple of each other
public static Boolean multipleOrFactor(String s1,
String s2)
{
// map store frequency of each character
Dictionary m1 = new Dictionary();
Dictionary m2 = new Dictionary();
for (int i = 0; i < s1.Length; i++)
{
if (m1.ContainsKey(s1[i]))
{
var x = m1[s1[i]];
m1[s1[i]]= ++x;
}
else
m1.Add(s1[i], 1);
}
for (int i = 0; i < s2.Length; i++)
{
if (m2.ContainsKey(s2[i]))
{
var x = m2[s2[i]];
m2[s2[i]]= ++x;
}
else
m2.Add(s2[i], 1);
}
foreach(KeyValuePair entry in m1)
{
// if any frequency is 0, then continue
// as condition is satisfied
if (!m2.ContainsKey(entry.Key))
continue;
// if factor or multiple, then condition satified
if (m2[entry.Key] != 0 &&
(m2[entry.Key] % entry.Value == 0 ||
entry.Value % m2[entry.Key] == 0))
continue;
// if condition not satisfied
else
return false;
}
return true;
}
// Driver code
public static void Main(String[] args)
{
String s1 = "geeksforgeeks", s2 = "geeks";
if (multipleOrFactor(s1, s2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
YES
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。