Remove Pattern Repeatedly
Programming Interview
Hard
6 views
Problem Description
Input provides {x}. Remove every occurrence of p from s while scanning left to right (like stack remove). Output final string.
Input Format
Two lines: s then p.
Output Format
One line result.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
s=lines[0].strip()
p=lines[1].strip()
if p=='':
sys.stdout.write(s)
sys.exit(0)
L=len(p)
stack=[]
for ch in s:
stack.append(ch)
if len(stack)>=L and stack[-L:]==list(p):
del stack[-L:]
sys.stdout.write(''.join(stack))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!