You are given two strings S and A of lengths N and M respectively.
String S contains characters from the set {?, a, b, c, d, e}.
String A contains characters from the set {a, b, c, d, e}.
Let S′ denote the string formed by replacing all the ? in S using the characters from the set {a, b, c, d, e}.
Construct S′ such that A is not a subsequence of S′.
If multiple such S′ exist, output any. If no such S′ exists, print −1.
EXPLANATION:
It is obvious that if string A is already a subsequence of string S then there is no way to construct S’ so return -1.
For when it is not a subsequence of S, we keep a track of how much of prefix substring of A has been found as subsequence as we traverse the string. When we encounter a ‘?’ we place a character in S’ which is not same as the next index of the prefix substring. Thus avoiding replacing of a same characters as in string A. Continue this till all ‘?’ have been processed which results in S’ which does not have A as a subsequence.
TIME COMPLEXITY:
O(|S|) for each test case as we traverse the string once.
ans will be -1 since A is already a subsequence of initial string S. (?ab?a) as mentioned in the first line of editorial.
Considering editorialist’s code , at some iteration we will have i = 5 and j = 2 and it checks first for the case j = m i.e. if we have got the second string as subsequence or not so the while loop will terminate at that point only printing -1 as output.
Edit: The tests must be fine since my solution is not wrong. Practically my solution passes every single test case, but it is difficult to prove the correctness of my solution.
(My first time writing something on cc)
1)if given string t is already is a subsequence of s then answer Is obviously -1
2)now, if t is not the subsequence…what we can do to replace ‘?’ in s so that we never gets the subsequence t? …we can replace the’ ? ’ with the character having least frequency in string t … Like if t=“ab”. Smallest least frequency character from set is ‘c’ hence we will replace ‘?’ with c…
This solution passed all test cases…
Some examples:-
S=“a?b?”
t=acb
Least frequency character in t is ‘d’ we will replace ? With d. …