# RHYDOOR - Editorial

**URL:** <https://discuss.codechef.com/t/rhydoor-editorial/126096>\
**Category:** editorial\
**Tags:** editorial, start262\
**Created:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/rhydoor-editorial/126096 "2026-10-28T17:30:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/rhydoor-editorial/126096/1 "2026-10-28T17:30:19Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/RHYDOOR)  
[Contest: Division 1](https://www.codechef.com/START262A/problems/RHYDOOR)  
[Contest: Division 2](https://www.codechef.com/START262B/problems/RHYDOOR)  
[Contest: Division 3](https://www.codechef.com/START262C/problems/RHYDOOR)  
[Contest: Division 4](https://www.codechef.com/START262D/problems/RHYDOOR)

_ **Author:** _ [nikhil\_010309](https://www.codechef.com/users/nikhil_010309)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

TBD

# PREREQUISITES:

None

# PROBLEM:

A door has two buttons, `A` and `B`.  
You are given a sequence of button presses.

If the same button is pressed three consecutive times, the door opens once.  
Pressing a different button resets the counter.  
Find the number of times the door was opened.

# EXPLANATION:

Simply simulate the process: store the current button being pressed and the number of presses, and update these appropriately with each new button press.

Whenever the counter hits 3, add 1 to the answer and reset the counter to 0.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n = int(input())
> s = input()
>     
> ans, cur, ct = 0, 'A', 0
> for c in s:
> if c == cur: ct += 1
> else:
> ct = 1
> cur = c
>         
> if ct == 3:
> ans += 1
> ct = 0
> print(ans)
> 
> ```
