Acacius and String (Codeforces 1379A)
Tags: Implement, strings

Acacius is studying strings theory. Today he came with the following problem.You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".Occurrence of a string $$$t$$$ of length $$$m$$$ in the string $$$s$$$ of length $$$n$$$ as a substring is a index $$$i$$$ ($$$1 \leq i \leq n - m + 1$$$) such that string $$$s[i..i+m-1]$$$ consisting of $$$m$$$ consecutive symbols of $$$s$$$ starting from $$$i$$$-th equals to string $$$t$$$. For example string "ababa" has two occurrences of a string "aba" as a substring with $$$i = 1$$$ and $$$i = 3$$$, but there are no occurrences of a string "aba" in the string "acba" as a substring.Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.

Input: First line of input contains an integer $$$T$$$ ($$$1 \leq T \leq 5000$$$), number of test cases. $$$T$$$ pairs of lines with test case descriptions follow.The first line of a test case description contains a single integer $$$n$$$ ($$$7 \leq n \leq 50$$$), length of a string $$$s$$$.The second line of a test case description contains string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks.

Output: For each test case output an answer for it.In case if there is no way to replace question marks in string $$$s$$$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".Otherwise output "Yes" and in the next line output a resulting string consisting of $$$n$$$ lowercase English letters. If there are multiple possible strings, output any.You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).

Note: In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.In sixth example there are two occurrences of a string "abacaba" as a substring.

Sample Inputs:
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba

Sample Outputs:
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No

Solution (Python):
import math
from collections import Counter, defaultdict
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
    n = int(input())
    s = input()
    s = s[:-1]
    ss = 'abacaba'
    if ss in s:
        if s.find(ss) != s.rfind(ss):
            print('NO')
            continue
        else:
            s = s.replace('?', 'z')
            print('YES')
            print(s)
            continue
    key = 0
    for i in range(n - 6):
        k = i
        j = 0
        ch = s[:i]
        while j < 7:
            if s[k] == ss[j]:
                ch = ch + s[k]
                k = k + 1
                j = j + 1
                continue
            elif s[k] == '?':
                ch = ch + ss[j]
                k = k + 1
                j = j + 1
                continue
            else:
                break
        else:
            ch = ch + s[i + 7:]
            if ch.find(ss) != ch.rfind(ss):
                continue
            else:
                ch = ch.replace('?', 'z')
                print('YES')
                print(ch)
                key = 1
                break
    if key == 0:
        print('NO')

Solution (Python):
import sys
import math
from bisect import bisect_right as br
from itertools import combinations as cb

def int_arr():
    return list(map(int, sys.stdin.readline().strip().split()))

def str_arr():
    return list(map(str, sys.stdin.readline().strip().split()))

def input():
    return sys.stdin.readline().strip()
for _ in range(int(input())):
    n = int(input())
    st = input()
    need = 'abacaba'
    if need in st:
        if st.find(need) != st.rfind(need):
            print('NO')
            continue
        else:
            st = st.replace('?', 'z')
            print('YES')
            print(st)
            continue
    flag = 1
    for i in range(n - 6):
        k = i
        j = 0
        ch = st[:i]
        while j < 7:
            if st[k] == need[j]:
                ch += st[k]
                k += 1
                j += 1
                continue
            elif st[k] == '?':
                ch += need[j]
                j += 1
                k += 1
                continue
            else:
                break
        else:
            ch += st[i + 7:]
            if ch.find(need) != ch.rfind(need):
                continue
            else:
                ch = ch.replace('?', 'z')
                print('YES')
                print(ch)
                flag = 0
                break
    if flag == 1:
        print('NO')

Solution (Python):
def main():
    n = int(input())
    instr = input()
    gstr = 'abacaba'
    cnt = 0
    for i in range(n - 6):
        if instr[i:i + 7] == gstr:
            cnt += 1
    if cnt > 1:
        return 'NO'
    elif cnt == 1:
        return ('YES', instr.replace('?', 'z'))
    for i in range(n - 6):
        poss = True
        cmp = instr[i:i + 7]
        for j in range(len(cmp)):
            if cmp[j] != gstr[j] and cmp[j] != '?':
                poss = False
        cnt = 0
        if poss:
            tststr = instr[:i] + gstr + instr[i + 7:]
            for j in range(len(tststr) - 6):
                if tststr[j:j + 7] == gstr:
                    cnt += 1
        if cnt == 1:
            return ('YES', tststr.replace('?', 'z'))
    return 'NO'
tests = int(input())
outs = []
for _ in range(tests):
    outs.append(main())
for out in outs:
    if out[0] == 'YES':
        print(out[0])
        print(out[1])
    else:
        print(out)