LeetCode Daily Challenge: Cycle Detection in Grid

🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gxcUfyyz The first logic that came to my mind after seeing the question was — this is a cycle detection problem. Since the path isn't fixed, it's an undirected cycle detection problem. Just applied that and voila — solved! The constraints and multiple possible paths gave a clear hint towards DFS. The grid is just an undirected graph — each cell is a node, and two adjacent cells are connected only if they share the same character. So the problem reduces to: does any connected component of same-character cells contain a cycle? Run DFS from every unvisited cell, tracking the parent to avoid treating the edge we came from as a back edge. If we hit an already-visited cell that isn't our direct parent — cycle found. ⏱️ Time: O(m × n) | Space: O(m × n) 👉 My Solution: https://lnkd.in/gATmwcnU If you found this helpful, feel free to ⭐ the repo or connect! 🙂 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari

  • text

To view or add a comment, sign in

Explore content categories