Editorial for String Remover
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
In this problem, we want to remove all occurrences of a space-seperated key in a string. This problem can be solved using the replace() function, as well as a few other string functions. First, check if the string contains " " + key + " ", if it does, replace it with a single space. Next, check if the start or end contains the key separated by a space. If it does, remove the key. However, we cannot check the substring if the key and string have the same length as we will get an index out of bounds exception, so we check if the key and string are the same length first. Finally, check if the key and string are equal to each other. If they are, print an empty string.
Pseudocode:
s,key = input()
while(s.contains(" " + key + " "):
s.replace(" " + key + " ", " ")
if(len(s) > len(key):
if(s.substring(0,len(key)+1) == key + " "):
s = s.substring(len(key)+1,len(s))
if(s.substring(len(s)-len(key)-1, len(s)) == " " + key):
s = s.substring(0, len(s)-len(key)-1)
if(s == key)
s = ""
print(s)
Comments