Check if a string is substring of another | int counter = 0 ; pointing s2 ; Special case where character preceding the i 'th character is duplicate ; System . out . println ( s2 . indexOf ( " for " ) ) ;
import java . io . * ; class GFG { public static int Substr ( String s2 , String s1 ) { int i = 0 ; for ( ; i < s1 . length ( ) ; i ++ ) { if ( counter == s2 . length ( ) ) break ; if ( s2 . charAt ( counter ) == s1 . charAt ( i ) ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ( ) ? - 1 : i - counter ; } public static void main ( String [ ] args ) { String s1 = " geeksfffffoorrfoorforgeeks " ; System . out . println ( Substr ( " for " , s1 ) ) ; } }