Check if a string can be split into substrings starting with N followed by N characters | Java 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
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 " ) ; } }