Explanation
Let us define as the final censored text.
We can iterate through every character of , appending it to . However, whenever we append a character we we need to check if ends with the censored word. If it does, we remove it from by taking off the last few characters.
As a demonstration, let's try this on the example case where:
Our solution will loop through each character of , appending it to until it becomes , when it will cut off the last 3 characters since they equal . This results in becoming . Right after this, becomes as the next letter in is , so we omit the last 3 characters again and becomes .
After this, the check isn't triggered anymore, so we end up with as our final word.
Implementation
Time Complexity:
import java.io.*;import java.util.*;public class Censor {public static void main(String[] args) throws IOException {Kattio io = new Kattio("censor");String s = io.next();String t = io.next();// We use StringBuilder in Java because it's significantly faster
Video Solution
By Amogha Pokkulandra
Video Solution Code
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!