Not able to implement it

Hello …
I am doing this question from codeforces…
https://codeforces.com/contest/1303/problem/C

Here i have figured out when the naswer will be “NO”… and also got the graph node and it’s neghibours in a map…
But i am not able to transform the node of that graph into string… I am confused as which should be left and which should be right…
I have thought that it call be implemented using DFS…
But not sure how to do that…
This is my code… till now…

you can see that i have prepared the map…which contains node and neghibours…
Please tell how to procedd further…
Thanks…

Hey @ashishnnnnn the use of map is clear to me as we will need it for sure, but use of set is not required if you are solving it with a greedy approach as i can see.
As we can maintain the current layout of the keyboard with letters that have already been encountered in the string, and then the current position on the layout.

If the next letter of the string is already on the layout, it must be adjacent to the current one, right? otherwise there is no answer.(which you have figured out as you are telling)

If there are no such letter yet, we can add it to the adjacent free position, if both of them is occupied, then there is no answer.

At the end, we have to add letters that were not in the string s.

Also keep this in mind
image
As it said that there won’t be any two adjacent character which will be equal.

What should we use instead of set in map…

Ek string le lo and name it S and initialize it with size of 1 having first character of the string s in all it’s indexes, or map toh chhiye he, thodi technical explanation hai isliye hinglish mai bol rha hu
map mai s[0] ko true mark krdo
fr joh string apne input krwayi hai like

(codedoca)

isme map[c] true ho gye hai, now initialize a variable pos =0
Now, just iterate on the remaining string using for loop
so now,

if (map[**any ith char**]){
//will check for three different conditions in it
          
 *// position can be changed and one step back of position is equal to s[i]
            if (pos > 0 && S[pos - 1] == s[i]) {
                pos--;
            }      

            *//position is less then the size of our S string and one ahead of position S is equal to s[i]
            else if (pos + 1 < sz(S) && S[pos + 1] == s[i]) {
                pos++;
            } 
            else {
                cout << "NO" << endl;
                return;
            }
        } 
        else {
*// our else block is responsible for appending the strings
*// Now check here if my pos is 0 or not, if it's then append
            if (pos == 0) {
                S = s[i] + S;
            }
*// here it will check if pos is size equal to my string S or not 
            else if (pos == sz(S) - 1) {
                S += s[i];
                pos++;
            } else {
                cout << "NO" << endl;
                return;
            }
        }

After doing this for i’th index of that string
make that i’th string as visited to by doing
map[any ith char] = true
So that next time when the same char encounter we can check. Though try to run this algo for different inputs, you will get the intuition, also if any barrier occurred cause of language then sorry for that too.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.