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.