Remove characters from a numeric string such that string becomes divisible by 8 | Java program to remove digits from a numeric string such that the number becomes divisible by 8 ; Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code
class GFG { static boolean checkSub ( String sub , String s ) { int j = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( sub . charAt ( j ) == s . charAt ( i ) ) j ++ ; return j == sub . length ( ) ; } static int getMultiple ( String s ) { for ( int i = 0 ; i < 1E3 ; i += 8 ) { if ( checkSub ( Integer . toString ( i ) , s ) ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { String s = "3454" ; System . out . println ( getMultiple ( s ) ) ; } }