Check if all occurrences of a character appear together | Javascript program to find if all occurrences of a character appear together in a string . ; function to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver code
function checkIfAllTogether ( s , c ) { function checkIfAllTogether ( s , c ) { let oneSeen = false ; let i = 0 , n = s . length ; while ( i < n ) { if ( s [ i ] == c ) { if ( oneSeen == true ) return false ; while ( i < n && s [ i ] == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } let s = " " ; if ( checkIfAllTogether ( s , ' ' ) ) document . write ( " " ) ; else document . write ( " " ) ;