给定一个大小为N的字符串str和一个整数K ,任务是检查输入字符串可以划分为大小为K 的子字符串,这些子字符串具有 ASCII 值的常数和。
例子:
Input: str = “abdcbbdba” K = 3
Output: YES
Explanation:
3 length substrings {“and”, “cbb”, “dba”} with sum of their ASCII values equal to 295.
Input: str = “ababcdabas” K = 5
Output : NO
Explanation :
5 length substrings {“ababc”, “dabas”} with sum of their ASCII values equal to 507.
方法:
请按照以下步骤解决问题:
- 检查N是否可以被K整除。如果N不能被K整除,那么不可能所有子串的长度都是 K。
- 计算所有长度为K 的子串的 ASCII 总和。如果所有子串只生成一个总和,则打印“YES”。
- 否则,打印“NO”。
下面是上述方法的实现:
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
Javascript
输出:
YES
时间复杂度: O (N)
辅助空间: O (1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live