Correct but slow solution for id:catalan