Today I was diving to a problem in CodeForce. Maybe because of my curiosity, I had a try in this problem named Not a Substring, which I shouldn’t touch.
Thinking Path
At the beginning, my thinking was brackets matching because I learnt how to match brackets using stack few days ago.
How to use this function? Of course list all of possible situations of ”(“and”)“. Then using this function to check all of situations and leave the valid ones.
Classic DFS. However, after testing, I found the brackets matching was useless. Because through this DFS, generated brackets strings must be valid. Anyway, the brackets strings had been generated. The next step is just find if the input is a substring of generated strings.
DanDan! Success is coming soon, the last step is easy, just call above functions in main function.
Input the example and then… success!!!!
Big Fail
When I submitted with laugher, the fact hit me a lot. Time out of limit!
To be honest, this is in my consideration. The time complexity of my code is O(2^n). I didn’t lost my belief. I tried to improve my match algorithm to KMP pattern searching.
However, from O(N*M) to O(N+M), do not influence the O(2^n) of DFS. Finally, I read the official answer and shocked me. So simple and perfect.
Maybe it’s why there is a great gab between me normal person and genius.😭😭😭