Split a given string into substrings of length K with equal sum of ASCII values
Last Updated :
08 Feb, 2024
Given a string str of size N and an integer K, the task is to check if the input string can be partitioned into substrings of size K having a constant sum of ASCII values.
Examples:
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.
Approach:
Follow the steps below to solve the problem:
- Check if N is divisible by K or not. If N is not divisible by K then it is not possible for all substrings to be of length K.
- Compute ASCII sum of all substrings of length K. If only a single sum is generated for all substrings, print "YES".
- Otherwise, print "NO".
Below is the implementation of the above approach :
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 <bits/stdc++.h>
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
<script>
// JavaScript 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
function check(str, K) {
// Check if the string can
// be split into substrings
// of K length only
if (str.length % K === 0) {
var sum = 0,
i;
// Compute the sum of first
// substring of length K
for (i = 0; i < K; i++) {
sum += str[i].charCodeAt(0);
}
// Compute the sum of
// remaining substrings
for (var j = i; j < str.length; j += K) {
var s_comp = 0;
for (var p = j; p < j + K; p++) s_comp += str[p].charCodeAt(0);
// 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
var K = 3;
var str = "abdcbbdba";
if (check(str, K)) document.write("YES");
else document.write("NO");
</script>
Time Complexity: O (N)
Auxiliary Space: O (1)
Similar Reads
Convert to a string that is repetition of a substring of k length Given a string, find if it is possible to convert it to a string that is the repetition of a substring with k characters. To convert, we can replace one substring of length k starting at index i (zero-based indexing) such that i is divisible by K, with k characters. Examples: Input: str = "bdac", k
7 min read
Minimize cost to convert given string into concatenation of equal substrings of length K Given a string S of length N consisting of lowercase letters and an integer K, where N % K = 0, the task is to find the minimum cost to convert the given string into a concatenated string of the same K-length substrings by performing the following operations: A character can be replaced with another
8 min read
Find number of substrings of length k whose sum of ASCII value of characters is divisible by k Given a string and a number k, the task is to find the number of substrings of length k whose sum of the ASCII value of characters is divisible by k. Examples: Input : str = "bcgabc", k = 3 Output : 2 Substring "bcg" has sum of ASCII values 300 and "abc" has sum of ASCII values 294 which are divisib
8 min read
Split given String into substrings of size K by filling elements Given a string str of length N and an integer K, the task is to split the string into K sized groups and if the last group does not have K characters remaining, then a character ch is used to complete the group. Examples: Input: str = "Algorithms", K = 3, ch = "@"Output: Alg ori thm s@@Explanation:T
5 min read
Count M-length substrings occurring exactly K times in a string Given a string S of length N and two integers M and K, the task is to count the number of substrings of length M occurring exactly K times in the string S. Examples: Input: S = "abacaba", M = 3, K = 2Output: 1Explanation: All distinct substrings of length 3 are "aba", "bac", "aca", "cab".Out of all
15+ min read