Editorial for Typo


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.

Authors: David

Solution

The text should be represented as an array of characters. Keep in mind that Java strings are immutable and the value cannot be changed without creating a new string. Arrays are different in that specific array indices can be changed at will. First, represent the text as a character array then for each typo, change the corresponding array index's value to the corrected character.
Example Java code:

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    // Get text as character array.
    char[] text = sc.nextLine().toCharArray();
    int typos = sc.nextInt();
    // For each typo, modify the array element to the corrected character.
    for (int i = 0; i < typos; i++) {
      int index = sc.nextInt();
      String replacement = sc.next();
      text[index] = replacement.charAt(0);
    }
    System.out.println(new String(text));
  }
}

Comments

There are no comments at the moment.