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++
#include <bits/stdc++.h>
using namespace std;
bool check(string str, int K)
{
if (str.size() % K == 0) {
int sum = 0, i;
for (i = 0; i < K; i++) {
sum += str[i];
}
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];
if (s_comp != sum)
return false ;
}
return true ;
}
return false ;
}
int main()
{
int K = 3;
string str = "abdcbbdba" ;
if (check(str, K))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
Java
class GFG{
static boolean check(String str, int K)
{
if (str.length() % K == 0 )
{
int sum = 0 , i;
for (i = 0 ; i < K; i++)
{
sum += str.charAt(i);
}
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);
if (s_comp != sum)
return false ;
}
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def check( str , K):
if ( len ( str ) % K = = 0 ):
sum = 0
for i in range (K):
sum + = ord ( str [i]);
for j in range (K, len ( str ), K):
s_comp = 0 ;
for p in range (j, j + K):
s_comp + = ord ( str [p]);
if (s_comp ! = sum ):
return False ;
return True ;
return False ;
K = 3 ;
str = "abdcbbdba" ;
if (check( str , K)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool check( string str, int K)
{
if (str.Length % K == 0)
{
int sum = 0, i;
for (i = 0; i < K; i++)
{
sum += str[i];
}
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];
if (s_comp != sum)
return false ;
}
return true ;
}
return false ;
}
public static void Main( string []args)
{
int K = 3;
string str = "abdcbbdba" ;
if (check(str, K))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function check(str, K) {
if (str.length % K === 0) {
var sum = 0,
i;
for (i = 0; i < K; i++) {
sum += str[i].charCodeAt(0);
}
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);
if (s_comp !== sum)
return false ;
}
return true ;
}
return false ;
}
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)
Share your thoughts in the comments
Please Login to comment...