Dada una string str de tamaño N y un entero K , la tarea es comprobar si la string de entrada se puede dividir en substrings de tamaño K que tengan una suma constante de valores ASCII.
Ejemplos:
Entrada: str = “abdcbbdba” K = 3
Salida: SI
Explicación:
3 substrings de longitud {“and”, “cbb”, “dba”} con suma de sus valores ASCII igual a 295.
Entrada: str = “ababcdabas” K = 5
Salida: NO
Explicación:
5 substrings de longitud {“ababc”, “dabas”} con la suma de sus valores ASCII igual a 507.
Enfoque:
siga los pasos a continuación para resolver el problema:
- Comprueba si N es divisible por K o no. Si N no es divisible por K , entonces no es posible que todas las substrings tengan una longitud K.
- Calcule la suma ASCII de todas las substrings de longitud K . Si solo se genera una sola suma para todas las substrings, imprima «SÍ».
- De lo contrario, escriba “NO”.
A continuación se muestra la implementación del enfoque anterior:
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>
YES
Complejidad Temporal: O (N)
Espacio Auxiliar: O (1)
Publicación traducida automáticamente
Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA