I have used DFS to solve the problem. I categorized the same set characters together and then did what is required. it is giving correct answer on all the given testcases even all the testcases until testcase 9.
It is giving Runtime error on testcase 9 probably because I have used Python to code. Can somebody help me to passed all the testcases with python.
Even I have changed the size of inbuilt stack using
sys.setrecursionlimit(10**9)
I have edited your code to increase the stack size. Just increasing the recursion depth doesn’t increase the stacksize automatically. You need to increase it using the thread function. Now it works in 0.8sec.
Also please avoid using recursion in python as much as you can, refer my solution for a more optimal approach: Submission #74960464 - Codeforces
Thanks for editing it.
I already looked at your solution and I get the point how you did this problem without dfs but I read someone’s comment at cf that this problem is doable with dfs or dsu.
then I started thinking about it and did it but continuously giving run time error at test case 9.