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++
#include <bits/stdc++.h>
using namespace std;
bool helper(string& s, int pos)
{
int len = s.size();
if (pos >= len)
return true ;
if (! isdigit (s[pos]))
return false ;
int num = 0;
for ( int i = pos; i < len; i++) {
num = num * 10 + s[pos] - '0' ;
if (i + 1 + num > len)
return false ;
if (helper(s, i + 1 + num))
return true ;
}
return false ;
}
int main()
{
string s = "123abc4db1c" ;
if (helper(s, 0))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
public static boolean helper(String s, int pos)
{
int len = s.length();
if (pos >= len)
return true ;
if (!Character.isDigit(s.charAt(pos)))
return false ;
int num = 0 ;
for ( int i = pos; i < len; i++)
{
num = num * 10 + s.charAt(pos) - '0' ;
if (i + 1 + num > len)
return false ;
if (helper(s, i + 1 + num))
return true ;
}
return false ;
}
public static void main (String[] args)
{
String s = "123abc4db1c" ;
if (helper(s, 0 ))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def helper(s, pos):
size = len (s)
if (pos > = size):
return True
if (s[pos].isdigit() = = False ):
return False
num = 0
for i in range (pos, size):
num = num * 10 + ord (s[pos]) - 48
if (i + 1 + num > size):
return False
if (helper(s, i + 1 + num)):
return True
return False
s = "123abc4db1c" ;
if (helper(s, 0 )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool helper(String s, int pos)
{
int len = s.Length;
if (pos >= len)
return true ;
if (! char .IsDigit(s[pos]))
return false ;
int num = 0;
for ( int i = pos; i < len; i++)
{
num = num * 10 + s[pos] - '0' ;
if (i + 1 + num > len)
return false ;
if (helper(s, i + 1 + num))
return true ;
}
return false ;
}
public static void Main(String[] args)
{
String s = "123abc4db1c" ;
if (helper(s, 0))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
function helper(s, pos) {
let size = s.length;
if (pos >= size) {
return true ;
}
if (!/^\d+$/.test(s[pos])) {
return false ;
}
let num = 0;
for (let i = pos; i < size; i++) {
num = num * 10 + parseInt(s[i]);
if (i + 1 + num > size) {
return false ;
}
if (helper(s, i + 1 + num)) {
return true ;
}
}
return false ;
}
let s = "123abc4db1c" ;
if (helper(s, 0)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Mar, 2023
Like Article
Save Article