Suppose we have a string s and another character c, we have to check whether all occurrences of c appear together in s or not. If the character c is not present in s then also return true.
So, if the input is like s = "bbbbaaaaaaaccddd", c = 'a', then the output will be True.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(string, c) : flag = False index = 0 n = len(string) while index < n: if string[index] == c: if (flag == True) : return False while index < n and string[index] == c: index += 1 flag = True else : index += 1 return True s = "bbbbaaaaaaaccddd" c = 'a' print(solve(s, c))