Check if all occurrences of a character appear together | 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
< ? php $ oneSeen = false ; $ i = 0 ; $ n = strlen ( $ s ) ; 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 ; } $ s = "110029" ; if ( checkIfAllTogether ( $ s , '1' ) ) echo ( " Yes STRNEWLINE " ) ; else echo ( " No STRNEWLINE " ) ; ? >