📌  相关文章
📜  检查一个字符串可以分成 3 个子字符串,使得其中一个是另外两个的子字符串

📅  最后修改于: 2021-10-27 09:08:00             🧑  作者: Mango

给定一个由N个小写字母组成的字符串S ,任务是检查是否可以将字符串S拆分为三个非空子字符串,使得Y是字符串XZ的子字符串。如果可以拆分字符串S,则打印“Yes” 。否则,打印“否”

例子:

接近:给定的问题是可以解决通过存储的唯一的字符的频率,并观察事实,如果存在具有频率至少为3的任何字符,则该字符串可以是分割成3串满足给定条件。请按照以下步骤解决问题:

  • 初始化一个数组,比如hash[] ,以存储字符串S中字符的频率。
  • 遍历字符串的字符和存储每个字符存在于字符串S在阵列散列的频率[]。
  • 遍历数组hash[] ,如果任一字符的频率大于2,则打印“Yes”并跳出循环。
  • 完成上述步骤后,如果不存在频率至少为3的字符,则打印“No”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if string S contains
// any character with frequency >= 3 or not
string freqCheck(string S, int N)
{
    // Stores frequency of characters
    int hash[26] = { 0 };
 
    // Iterate over the string
    for (int i = 0; i < N; i++) {
 
        // Update the frequency
        // of current character
        hash[S[i] - 'a']++;
    }
 
    // Iterate over the hash array
    for (int i = 0; i < 26; i++) {
 
        // If any character has
        // frequency >= 3
        if (hash[i] > 2) {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
{
    string S = "geekseekforgeeks";
    int N = S.length();
    cout << freqCheck(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if string S contains
// any character with frequency >= 3 or not
static String freqCheck(String S, int N)
{
     
    // Stores frequency of characters
    int hash[] = new int[26];
 
    // Iterate over the string
    for(int i = 0; i < N; i++)
    {
         
        // Update the frequency
        // of current character
        hash[S.charAt(i) - 'a']++;
    }
 
    // Iterate over the hash array
    for(int i = 0; i < 26; i++)
    {
         
        // If any character has
        // frequency >= 3
        if (hash[i] > 2)
        {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "geekseekforgeeks";
    int N = S.length();
     
    System.out.println(freqCheck(S, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to check if string S contains
# any character with frequency >= 3 or not
def freqCheck(S, N):
 
    # Stores frequency of characters
    hash = [0] * 26
 
    # Iterate over the string
    for i in range(N):
 
        # Update the frequency
        # of current character
        hash[ord(S[i]) - ord('a')] += 1
 
    # Iterate over the hash array
    for i in range(26):
 
        # If any character has
        # frequency >= 3
        if (hash[i] > 2):
            return "Yes"
 
    # Otherwise
    return "No"
 
# Driver Code
if __name__ == "__main__":
 
    S = "geekseekforgeeks"
    N = len(S)
     
    print(freqCheck(S, N))
     
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if string S contains
// any character with frequency >= 3 or not
static string freqCheck(string S, int N)
{
     
    // Stores frequency of characters
    int[] hash = new int[26];
 
    // Iterate over the string
    for(int i = 0; i < N; i++)
    {
         
        // Update the frequency
        // of current character
        hash[S[i] - 'a']++;
    }
 
    // Iterate over the hash array
    for(int i = 0; i < 26; i++)
    {
         
        // If any character has
        // frequency >= 3
        if (hash[i] > 2)
        {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver code
static public void Main()
{
    string S = "geekseekforgeeks";
    int N = S.Length;
     
    Console.WriteLine(freqCheck(S, N));
}
}
 
// This code is contributed by offbeat


Javascript


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程