Check if a string is substring of another | var counter = 0 ; pointing s2 ; Special case where character preceding the i 'th character is duplicate ; Driver code
< ! -- Javascript program for the above approach -- > function Substr ( s2 , s1 ) { var i = 0 ; for ( ; i < s1 . length ; i ++ ) { if ( counter == s2 . length ) { break ; } if ( s2 [ counter ] == s1 [ i ] ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ? - 1 : i - counter ; } var s1 = " " ; document . write ( Substr ( " " , s1 ) ) ; < ! -- this code is contributed by Nirajgusain5 -- >