📌  相关文章
📜  将给定的字符串拆分为长度为K的子字符串,并包含相等的ASCII值

📅  最后修改于: 2021-06-25 18:58:11             🧑  作者: Mango

给定大小为N的字符串str和整数K ,任务是检查输入字符串可以划分为大小为K的子字符串,这些子字符串具有恒定的ASCII值之和。

例子:

方法:
请按照以下步骤解决问题:

  1. 检查N是否可被K整除。如果N不能被K整除,则不可能所有子串的长度都为K。
  2. 计算长度为K的所有子串的ASCII总和。如果所有子字符串仅生成一个总和,则打印“是”。
  3. 否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program to check if a given
// string can be split into substrings
// of size K having an equal sum of
// ASCII values.
#include 
using namespace std;
  
// Function for checking string
bool check(string str, int K)
{
    // Check if the string can
    // be split into substrings
    // of K length only
    if (str.size() % K == 0) {
        int sum = 0, i;
  
        // Compute the sum of first
        // substring of length K
        for (i = 0; i < K; i++) {
            sum += str[i];
        }
        // Compute the sum of
        // remaining substrings
        for (int j = i; j < str.size();
             j += K) {
            int s_comp = 0;
            for (int p = j; p < j + K;
                 p++)
                s_comp += str[p];
            // Check if sum is equal
            // to that of the first
            // substring
            if (s_comp != sum)
                // Since all sums are not
                // equal, return false
                return false;
        }
        // All sums are equal,
        // Return true
        return true;
    }
    // All substrings cannot
    // be of size K
    return false;
}
// Driver Program
int main()
{
  
    int K = 3;
    string str = "abdcbbdba";
  
    if (check(str, K))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}


Java
// Java program to check if a given
// string can be split into substrings
// of size K having an equal sum of
// ASCII values.
class GFG{
  
// Function for checking string
static boolean check(String str, int K)
{
      
    // Check if the string can
    // be split into substrings
    // of K length only
    if (str.length() % K == 0)
    {
        int sum = 0, i;
  
        // Compute the sum of first
        // substring of length K
        for(i = 0; i < K; i++)
        {
           sum += str.charAt(i);
        }
          
        // Compute the sum of
        // remaining substrings
        for(int j = i; j < str.length(); j += K) 
        {
           int s_comp = 0;
           for(int p = j; p < j + K; p++)
              s_comp += str.charAt(p);
                
           // Check if sum is equal
           // to that of the first
           // substring
           if (s_comp != sum)
                 
               // Since all sums are not
               // equal, return false
               return false;
        }
          
        // All sums are equal,
        // Return true
        return true;
    }
      
    // All substrings cannot
    // be of size K
    return false;
}
  
// Driver code
public static void main(String args[])
{
    int K = 3;
    String str = "abdcbbdba";
  
    if (check(str, K))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by rock_cool


Python3
# Python3 program to check if a given 
# string can be split into substrings 
# of size K having an equal sum of 
# ASCII values. 
  
# Function for checking string 
def check(str, K): 
  
    # Check if the string can 
    # be split into substrings 
    # of K length only 
    if (len(str) % K == 0): 
        sum = 0
          
        # Compute the sum of first 
        # substring of length K 
        for i in range(K): 
            sum += ord(str[i]); 
          
        # Compute the sum of 
        # remaining substrings 
        for j in range(K, len(str), K): 
            s_comp = 0; 
            for p in range(j, j + K):
                s_comp += ord( str[p]); 
                  
            # Check if sum is equal 
            # to that of the first 
            # substring 
            if (s_comp != sum):
                  
                # Since all sums are not 
                # equal, return False 
                return False; 
          
        # All sums are equal, 
        # Return true 
        return True; 
      
    # All substrings cannot 
    # be of size K 
    return False; 
  
# Driver code 
K = 3; 
str = "abdcbbdba"; 
  
if (check(str, K)):
    print("YES")
else:
    print("NO")
  
# This is code contributed by grand_master


C#
// C# program to check if a given
// string can be split into substrings
// of size K having an equal sum of
// ASCII values.
using System;
class GFG{
  
// Function for checking string
static bool check(string str, int K)
{
      
    // Check if the string can
    // be split into substrings
    // of K length only
    if (str.Length % K == 0)
    {
        int sum = 0, i;
  
        // Compute the sum of first
        // substring of length K
        for(i = 0; i < K; i++)
        {
            sum += str[i];
        }
          
        // Compute the sum of
        // remaining substrings
        for(int j = i; j < str.Length; j += K) 
        {
            int s_comp = 0;
            for(int p = j; p < j + K; p++)
                s_comp += str[p];
                      
            // Check if sum is equal
            // to that of the first
            // substring
            if (s_comp != sum)
                      
                // Since all sums are not
                // equal, return false
                return false;
        }
          
        // All sums are equal,
        // Return true
        return true;
    }
      
    // All substrings cannot
    // be of size K
    return false;
}
  
// Driver code
public static void Main(string []args)
{
    int K = 3;
    string str = "abdcbbdba";
  
    if (check(str, K))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by Ritik Bansal


输出:
YES

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。