Check if a string can be split into substrings starting with N followed by N characters | C ++ Program to implement the above approach ; Function to check if the given can be split into desired substrings ; Length of the string ; Traverse the string ; Extract the digit ; Check if the extracted number does not exceed the remaining length ; Check for the remaining string ; If generating desired substrings is not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 " ; }