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).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace all occurrences of character X with character Y in given string
- Largest sub-string where all the characters appear at least K times
- Largest substring where all characters appear at least K times | Set 2
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Remove all occurrences of a character in a string
- Minimize ASCII values sum after removing all occurrences of one character
- Remove all occurrences of a character in a string | Recursive approach
- Array elements that appear more than once
- Remove characters that appear more than k times
- Remove elements that appear strictly less than k times
- Remove elements from the array which appear more than k times
- Distinct state codes that appear in a string as contiguous sub-strings
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Count words that appear exactly two times in an array of words
- Given a sequence of words, print all anagrams together | Set 2
- Minimum Swaps required to group all 1's together
- Minimum swaps required to bring all elements less than or equal to k together
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.