- 
                Notifications
    You must be signed in to change notification settings 
- Fork 266
Consecutive Characters
        LeWiz24 edited this page Aug 22, 2025 
        ·
        3 revisions
      
    Unit 3 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What if the string is empty?
- In that case, the count would be 0.
 
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop through the string, keeping track of both the current consecutive count and the maximum found so far.
1) If string is empty, return 0
2) Assume max count is 1 and current count is 1
3) For each character in the string (skipping the first)
  a) If the character matches the one before it
     i) add 1 to current
    ii) if current > max, update max 
  b) If no match, reset current to 1
4) Return maxdef count_consecutive_characters(str1):
    if not str1:
        return 0
    max_count = 1
    current_count = 1
    for i in range(1, len(str1)):
        if str1[i] == str1[i-1]:
            current_count += 1
            max_count = max(max_count, current_count)
        else:
            current_count = 1
    return max_count