# Hackerearth dfs problem

Can any one explain the logic given in editorial https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/gudi-trapped-in-the-room-july-easy/

2 Likes

Copy paste to your browser and remove the square bracket at end of link. Should work like a charm! (This error happens quite often XD)

1 Like

2 Likes

Here is logic of how we can solve this problem.
As it is mentioned in the question itself that “We can apply spell any number of times in any order”,We have to try for every possible string and first apply only H number of shifts and then apply dfs if it has n’t been applied for the string before,It will try for every string when shift operations has been applied first and then add A to even positions of string and then apply dfs if it has n’t been applied before for that string,This will try for every string when add operation has been applied.In this way we are making operations to the string with every possible way and checking for lexicographically smallest string.