Count of occurrences of a "1(0 + ) 1" pattern in a string | Returns count of occurrences of "1(0 + ) 1" int str . ; let count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver program to test above function
function countPattern ( str ) { let len = str . length ; let oneSeen = false ; for ( let i = 0 ; i < len ; i ++ ) { let getChar = str [ i ] ; if ( getChar == ' ' && oneSeen == true ) { if ( str [ i - 1 ] == ' ' ) count ++ ; } if ( getChar == ' ' && oneSeen == false ) oneSeen = true ; if ( getChar != ' ' && str [ i ] != ' ' ) oneSeen = false ; } return count ; } let str = " " ; document . write ( countPattern ( str ) ) ;