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 conditionInput: str = “31ba2a”
Output: No
Explanation:
The entire string cannot be split into substrings of desired types
Approach:
-
Follow the steps below to solve the problem:
- 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.
- Check if a string can be split into even length palindromic substrings
- Remove minimum characters from string to split it into three substrings under given constraints
- Check if a string can be split into two strings with same number of K-frequent characters
- Split the string into substrings using delimiter
- Split the binary string into substrings with equal number of 0s and 1s
- Split a given string into substrings of length K with equal sum of ASCII values
- Count ways to split a Binary String into three substrings having equal count of zeros
- Check if given string can be split into four distinct strings
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Check if an array can be split into 3 subsequences of equal sum or not
- Substrings starting with vowel and ending with consonants and vice versa
- Check If every group of a's is followed by a group of b's of same length
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Decode a string recursively encoded as count followed by substring
- Rearrange a string in sorted order followed by the integer sum
- Rearrange a string in the form of integer sum followed by the minimized character
- String formed with middle character of every right substring followed by left sequentially
- Count of Substrings that can be formed without using the given list of Characters
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
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 |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

