Check if all occurrences of a character appear together | Java 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
import java . io . * ; class GFG { static boolean checkIfAllTogether ( String s , char c ) { boolean oneSeen = false ; int i = 0 , n = s . length ( ) ; while ( i < n ) { if ( s . charAt ( i ) == c ) { if ( oneSeen == true ) return false ; while ( i < n && s . charAt ( i ) == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } public static void main ( String [ ] args ) { String s = "110029" ; if ( checkIfAllTogether ( s , '1' ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }