📌  相关文章
📜  检查一个字符串可以分为3个子字符串,以便其中一个是其他两个的子字符串

📅  最后修改于: 2021-04-17 18:32:33             🧑  作者: Mango

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

例子:

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

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

下面是上述方法的实现:

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


输出:
Yes

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