Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true.
Input: s = "1110000323", c = '1' Output: Yes All occurrences of '1' appear together in "1110000323" Input: s = "3231131", c = '1' Output: No All occurrences of 1 are not together Input: s = "abcabc", c = 'c' Output: No All occurrences of 'c' are not together Input: s = "ababcc", c = 'c' Output: Yes All occurrences of 'c' are together
The idea is to traverse given string, as soon as we find an occurrence of c, we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
The complexity of above program is O(n).
- Map every character of one string to another such that all occurrences are mapped to the same character
- Remove all occurrences of a character in a string
- Count occurrences of a character in a repeated string
- Print the string by ignoring alternate occurrences of any character
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Minimize ASCII values sum after removing all occurrences of one character
- Find the number of occurrences of a character upto preceding position
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check if there is any common character in two given strings
- Check if both halves of the string have at least one different character
- Check whether the Average Character of the String is present or not
- Check for Palindrome after every character replacement Query
- Check if a two character string can be made using given words
- Check if the frequency of any character is more than half the length of the string
- Check if max occurring character of one string appears same no. of times in other
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.