检查是否存在仅包含 2 个不同字符且频率为其他字符两倍的子字符串
给定一个包含N个小写英文字母的字符串str[] ,任务是检查是否存在给定字符串的子字符串,使得该子字符串仅由两个字符组成,并且第一个字符的频率 = 2 * 频率2 nd字符。
例子:
Input: str[] = “aaaabbc”
Output: Yes
Explanation: The substring str[0… 5] = “aaaabb” of the given string is composed of only two characters ‘a’ and ‘b’ and frequency of ‘a’ = 2 * frequency of ‘b’, i.e, 4 = 2 * 2. Another example of a valid substring is str[4… 6] = “bbc”.
Input: str[] = “abcdefg”
Output: No
朴素方法:给定问题可以通过迭代给定字符串的所有子字符串并检查字符串是否仅由两个字符组成并且第一个字符的频率= 2 *第二个字符的频率来解决。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效方法:上述方法可以使用贪心技术进行优化。仔细观察可以发现,要使任何子串有效,必须存在该串的大小为3的子字符串,且该子串仅由两个字符组成,且第一个字符的字符= 2 *第二个字符。因此,给定的问题可以通过迭代给定的字符串来解决,对于每个长度为3的子字符串,检查是否存在“ xxy ”、“ xyx ”或“ yxx ”形式的字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if there exist a
// substring such that it is made up of
// only two characters and freq of 1st
// char is equal to 2 * freq of 2nd char
bool isPossible(string str)
{
// Loop to iterate over the string
for (int i = 0; i + 2 < str.size(); i++) {
// If the string is of
// the form "xxy"
if (str[i] == str[i + 1]
&& str[i] != str[i + 2]) {
return true;
}
// If the string is of
// the form "xyx"
if (str[i] == str[i + 2]
&& str[i] != str[i + 1]) {
return true;
}
// If the string is of
// the form "yxx"
if (str[i + 1] == str[i + 2]
&& str[i] != str[i + 1]) {
return true;
}
}
// If no valid substring exist
return false;
}
// Driver Code
int main()
{
string str = "aaaabbc";
if (isPossible(str))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to check if there exist a
// substring such that it is made up of
// only two characters and freq of 1st
// char is equal to 2 * freq of 2nd char
public static boolean isPossible(String str)
{
// Loop to iterate over the string
for (int i = 0; i + 2 < str.length(); i++) {
// If the string is of
// the form "xxy"
if (str.charAt(i) == str.charAt(i + 1)
&& str.charAt(i) != str.charAt(i + 2)) {
return true;
}
// If the string is of
// the form "xyx"
if (str.charAt(i) == str.charAt(i + 2)
&& str.charAt(i) != str.charAt(i + 1)) {
return true;
}
// If the string is of
// the form "yxx"
if (str.charAt(i + 1) == str.charAt(i + 2)
&& str.charAt(i) != str.charAt(i + 1)) {
return true;
}
}
// If no valid substring exist
return false;
}
// Driver Code
public static void main(String args[]) {
String str = "aaaabbc";
if (isPossible(str))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python3 program for the above approach
# Function to check if there exist a
# substring such that it is made up of
# only two characters and freq of 1st
# char is equal to 2 * freq of 2nd char
def isPossible(str):
# Loop to iterate over the string
for i in range(0, len(str) - 2):
# If the string is of
# the form "xxy"
if (str[i] == str[i + 1] and
str[i] != str[i + 2]):
return True
# If the string is of
# the form "xyx"
if (str[i] == str[i + 2] and
str[i] != str[i + 1]):
return True
# If the string is of
# the form "yxx"
if (str[i + 1] == str[i + 2] and
str[i] != str[i + 1]):
return True
# If no valid substring exist
return False
# Driver Code
if __name__ == "__main__":
str = "aaaabbc"
if (isPossible(str)):
print("Yes")
else:
print("No")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if there exist a
// substring such that it is made up of
// only two characters and freq of 1st
// char is equal to 2 * freq of 2nd char
public static bool isPossible(String str)
{
// Loop to iterate over the string
for (int i = 0; i + 2 < str.Length; i++)
{
// If the string is of
// the form "xxy"
if (str[i] == str[i + 1]
&& str[i] != str[i + 2])
{
return true;
}
// If the string is of
// the form "xyx"
if (str[i] == str[i + 2]
&& str[i] != str[i + 1])
{
return true;
}
// If the string is of
// the form "yxx"
if (str[i + 1] == str[i + 2]
&& str[i] != str[i + 1])
{
return true;
}
}
// If no valid substring exist
return false;
}
// Driver Code
public static void Main()
{
String str = "aaaabbc";
if (isPossible(str))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
Yes
时间复杂度: O(N)
辅助空间: O(1)