Check if a string can be split into substrings starting with N followed by N characters Last Updated : 08 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a string str, the task is to check if it can be split into substrings such that each substring starts with a numeric value followed by a number of characters represented by that numeric integer. Examples: Input: str = "4g12y6hunter" Output: Yes Explanation: Substrings "4g12y" and "6hunter" satisfy the given condition Input: str = "31ba2a" Output: No Explanation: The entire string cannot be split into substrings of desired types Approach: Check for the conditions when a split is not possible:If the given string does not start with a number.If the integer, in the beginning of a substring, is greater than the total number of succeeding characters in the remaining substring.If the above two condition are not satisfied, an answer is definitely possible. Hence find the substrings recursively. Below is the implementation of the above approach: C++ // C++ Program to implement the // above approach #include <bits/stdc++.h> using namespace std; // Function to check if the given // can be split into desired // substrings bool helper(string& s, int pos) { // Length of the string int len = s.size(); if (pos >= len) return true; if (!isdigit(s[pos])) return false; int num = 0; // Traverse the string for (int i = pos; i < len; i++) { // Extract the digit num = num * 10 + s[pos] - '0'; // Check if the extracted number // does not exceed the remaining // length if (i + 1 + num > len) return false; // Check for the remaining // string if (helper(s, i + 1 + num)) return true; } // If generating desired // substrings is not possible return false; } // Driver Code int main() { string s = "123abc4db1c"; if (helper(s, 0)) cout << "Yes"; else cout << "No"; } Java // Java program to implement the // above approach import java.util.*; class GFG{ // Function to check if the given // can be split into desired // substrings public static boolean helper(String s, int pos) { // Length of the string int len = s.length(); if (pos >= len) return true; if (!Character.isDigit(s.charAt(pos))) return false; int num = 0; // Traverse the string for(int i = pos; i < len; i++) { // Extract the digit num = num * 10 + s.charAt(pos) - '0'; // Check if the extracted number // does not exceed the remaining // length if (i + 1 + num > len) return false; // Check for the remaining // string if (helper(s, i + 1 + num)) return true; } // If generating desired // substrings is not possible return false; } // Driver code public static void main (String[] args) { String s = "123abc4db1c"; if (helper(s, 0)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by divyeshrabadiya07 Python3 # Python3 program to implement the # above approach # Function to check if the given # can be split into desired # substrings def helper(s, pos): # Length of the string size = len(s) if(pos >= size): return True if(s[pos].isdigit() == False): return False num = 0 # Traverse the string for i in range(pos, size): # Extract the digit num = num * 10 + ord(s[pos]) - 48 # Check if the extracted number # does not exceed the remaining # length if (i + 1 + num > size): return False # Check for the remaining # string if (helper(s, i + 1 + num)): return True # If generating desired # substrings is not possible return False # Driver Code s = "123abc4db1c"; if (helper(s, 0)): print("Yes") else: print("No") # This code is contributed by Sanjit_Prasad C# // C# program to implement the // above approach using System; class GFG{ // Function to check if the given // can be split into desired // substrings public static bool helper(String s, int pos) { // Length of the string int len = s.Length; if (pos >= len) return true; if (!char.IsDigit(s[pos])) return false; int num = 0; // Traverse the string for(int i = pos; i < len; i++) { // Extract the digit num = num * 10 + s[pos] - '0'; // Check if the extracted number // does not exceed the remaining // length if (i + 1 + num > len) return false; // Check for the remaining // string if (helper(s, i + 1 + num)) return true; } // If generating desired // substrings is not possible return false; } // Driver code public static void Main(String[] args) { String s = "123abc4db1c"; if (helper(s, 0)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by amal kumar choubey JavaScript // JavaScript program to implement the above approach // Function to check if the given // can be split into desired // substrings function helper(s, pos) { // Length of the string let size = s.length; if (pos >= size) { return true; } if (!/^\d+$/.test(s[pos])) { return false; } let num = 0; // Traverse the string for (let i = pos; i < size; i++) { // Extract the digit num = num * 10 + parseInt(s[i]); // Check if the extracted number // does not exceed the remaining // length if (i + 1 + num > size) { return false; } // Check for the remaining // string if (helper(s, i + 1 + num)) { return true; } } // If generating desired // substrings is not possible return false; } // Driver Code let s = "123abc4db1c"; if (helper(s, 0)) { console.log("Yes"); } else { console.log("No"); } //contributed by adityasha4x71 OutputYes Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if a string can be split into substrings starting with N followed by N characters coder001 Follow Improve Article Tags : Strings Pattern Searching Recursion Data Structures DSA substring +2 More Practice Tags : Data StructuresPattern SearchingRecursionStrings Similar Reads Check if a string can be split into two substrings with equal number of vowels Given a string S, the task is to check if the string can be split into two substrings such that the number of vowels in both of them are equal. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "geeks"Output: YesExplanation: Splitting the strings into substrings "ge" 15+ min read Check if characters of a given string can be used to form any N equal strings Given a string S and an integer N, the task is to check if it is possible to generate any string N times from the characters of the given string or not. If it is possible, print Yes. Otherwise, print No. Examples: Input: S = "caacbb", N = 2Output: YesExplanation: All possible strings that can be gen 5 min read Check if a two character string can be made using given words Given a string of two characters and n distinct words of two characters. The task is to find if it is possible to arrange given words in such a way that the concatenated string has the given two character string as a substring. We can append a word multiple times. Examples: Input : str = "ya" words[ 6 min read Check if a string can be split into two substrings such that one substring is a substring of the other Given a string S of length N, the task is to check if a string can be split into two substrings, say A and B such that B is a substring of A. If not possible, print No. Otherwise, print Yes. Examples : Input: S = "abcdab"Output: YesExplanation: Considering the two splits to be A="abcd" and B="ab", B 5 min read Check if binary representations of 0 to N are present as substrings in given binary string Give binary string str and an integer N, the task is to check if the substrings of the string contain all binary representations of non-negative integers less than or equal to the given integer N. Examples: Input: str = â0110", N = 3 Output: True Explanation: Since substrings â0", â1", â10", and â11 9 min read Check if given String can be split only into subsequences ABC Given a string S of length N where each character of the string is either 'A', 'B' or 'C'. The task is to find if is it possible to split the string into subsequences "ABC". If it is possible to split then print "Yes". Otherwise, print "No". Examples: Input: S = "ABABCC"Output: YesExplanation:One of 12 min read Lengths of maximized partitions of a string such that each character of the string appears in one substring Given string str of lowercase alphabets, split the given string into as many substrings as possible such that each character from the given string appears in a single substring. The task is to print the length of all such partitions. Examples: Input: str = "acbbcc"Output: 1 5Explanation:Possible par 8 min read Count of Substrings that can be formed without using the given list of Characters Given a string str and a list of characters L, the task is to count the total numbers of substrings of the string str without using characters given in the list L. Examples: Input: str = "abcd", L[] = {'a', 'b', 't', 'q'} Output: 3 Explanation: On ignoring the characters 'a' and 'b' from the given s 7 min read Find if a given string can be represented from a substring by iterating the substring ânâ times Given a string 'str', check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Examples: Input: str = "abcabcabc" Output: true The given string is 3 times repetition of "abc" Input: str = "abadabad" Output: true The given string is 2 times r 15+ min read Check if given string can be split into four distinct strings Given a string, the task is to check if we can split it into 4 strings such that each string is non-empty and different from the other.Examples: Input : str[] = "geeksforgeeks"Output : Yes"geeks", "for", "gee", "ks" are four distinct strings that can form from given string.Input : str[] = "aaabb"Out 7 min read Like