String related problem using python





I am trying to solve this problem but i cannot figure out how to fix this?Can you please help me?
My code so far:

for _ in range(int(input())):
    n = int(input())
    s = input()
    m = 'abacaba'
    k = 0
    li = []
    for x in range(len(m)):
        if m[x] == s[x] or s[x] == '?':
            li.append(m[x])
        else:
            li.append(s[x])
    # i don't know, am i in the right way to solve this

I try this in codeforces.Here is the original link of this problem.

Hmm, I’d be tempted to try this using regex and a loop.

I haven’t given this a ton of thought, and my knowledge of pythonesque regex is limited, but I toyed with some strings and a naive regex pattern on regex101.com

Sorry for image, I’m on mobile.

You can see a rough pattern here and that it works for abacaba strings, but this doesn’t account for the ‘exactly one’ requirement - that’s where a loop might come in (or just a simple count of the number of abacaba substrings might work, now that I think about it).

It’s been a while since I regexed with Python so I can’t confirm the syntax is the same as JS.

It’s probably not the best way to do this, but it’s the first idea that occurred to me.

Edit: just noticed my regex pattern in the image has a rogue . in there you should ignore!

1 Like