Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

board =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Only regions connected to the border should stay 'O'.

Approach

  1. BFS/DFS from all border 'O' cells, mark them as temporary 'T'.
  2. Flip remaining 'O' to 'X'.
  3. Convert 'T' back to 'O'.

Why it works

An 'O' is surrounded iff it is not connected (4-dir) to any border 'O'.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)
Solution
Updated: 2026-02-23

Idea

Only regions connected to the border should stay 'O'.

Approach

  1. BFS/DFS from all border 'O' cells, mark them as temporary 'T'.
  2. Flip remaining 'O' to 'X'.
  3. Convert 'T' back to 'O'.

Why it works

An 'O' is surrounded iff it is not connected (4-dir) to any border 'O'.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)