Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if all occurrences of a character appear together in Python
Sometimes we need to check if all occurrences of a specific character appear consecutively in a string. This means the character should form a single continuous block without being scattered throughout the string.
For example, in the string "bbbbaaaaaaaccddd", all occurrences of 'a' appear together as one block, so the result is True. However, in "bbbaaacccaaaddd", the character 'a' appears in two separate groups, so the result would be False.
Algorithm Approach
We can solve this using a flag-based approach ?
- Use a
flagto track if we've already seen a block of the target character - When we encounter the character, check if we've seen it before (flag is True)
- If yes, return False (character appears in multiple blocks)
- Otherwise, skip all consecutive occurrences and set the flag
Implementation
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
# Test with the given example
s = "bbbbaaaaaaaccddd"
c = 'a'
print(f"String: {s}")
print(f"Character: {c}")
print(f"All occurrences together: {solve(s, c)}")
String: bbbbaaaaaaaccddd Character: a All occurrences together: True
Testing with Different Cases
Let's test with various scenarios to understand the behavior ?
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
# Test cases
test_cases = [
("bbbbaaaaaaaccddd", 'a'), # All 'a's together
("bbbaaacccaaaddd", 'a'), # 'a's in separate groups
("xyzabc", 'z'), # Character not present
("aaabbbccc", 'b'), # All 'b's together
("abcabc", 'c') # 'c's in separate positions
]
for string, char in test_cases:
result = solve(string, char)
print(f"'{string}' with '{char}': {result}")
'bbbbaaaaaaaccddd' with 'a': True 'bbbaaacccaaaddd' with 'a': False 'xyzabc' with 'z': True 'aaabbbccc' with 'b': True 'abcabc' with 'c': False
Alternative Approach Using String Methods
We can also solve this using Python's built-in string methods ?
def check_consecutive(string, c):
if c not in string:
return True
# Find first and last occurrence
first = string.find(c)
last = string.rfind(c)
# Check if all characters between first and last are 'c'
substring = string[first:last+1]
return all(char == c for char in substring)
# Test the alternative approach
test_string = "bbbbaaaaaaaccddd"
test_char = 'a'
result = check_consecutive(test_string, test_char)
print(f"Alternative method result: {result}")
Alternative method result: True
Conclusion
Both approaches effectively check if all occurrences of a character appear consecutively. The flag-based method processes the string in a single pass, while the string method approach uses built-in functions for a more concise solution.
