Similar to this
https://leetcode.com/problems/number-of-islands/
and
https://leetcode.com/problems/reorder-data-in-log-files/