That is because of weak test cases. Also whenever you post a piece of code, format it.
And I was wrong about the complexity. As @dhruv788 has mentioned, you’re function is O(n^2), and you’re calling it n times. So the overall complexity is O(n^3). On top of that, if we add collisions in your map, it might go up to O(n^4).