TopCoder problem "AutoFix" used in TCCC '04 Semifinals 3 (Division I Level One)

Problem Statement

    Modern word processors often auto-correct certain commonly occurring typos. For example, if you type in "teh" it might be automatically corrected to "the". Sometimes, this can be annoying if you really want to type "teh" (perhaps it is an acronym). In this problem we are going to correct a specific kind of typing error. We will be looking for instances where someone types in two words (a word is a sequence of letters that is surrounded on both sides by a space and/or the edge of the String) as "xxxxx xyyyyy", when they meant "xxxxxx yyyyy". Here, "xxxxxx" and "yyyyy" represent two words in our dictionary. For example, if someone types in "th eproblem", we might correct it to "the problem". But, to avoid the annoying situation mentioned above, we will only make the correction when the last letter of the first word is typed 20 milliseconds or less after the preceding space. In other words, we want to make the correction only when a person almost types the characters in the right order, but his timing is just a little off. Furthermore, we only want to make the correction if at least one of the two words is not already in our dictionary, and both of the words after the transformation are in our dictionary. Thus, for example, if "th" and "eproblem" were both in the dictionary as valid words, we would not make the transformation, even if "the" and "problem" were also in the dictionary.

You will be given a String, sentence, representing the characters typed. You will also be given a int[], times representing the times (in milliseconds) since the start of typing that each key was pressed. Each character in sentence corresponds to the element of times with the same index. You will also be given a String[], dictionary, which represents a list of words (all in lowercase). Your task is to make the transformation mentioned above, ignoring the case of the characters in sentence, and starting from the beginning of sentence and moving forward. Though you should ignore case in determining whether or not to make the transformation, you should still preserve the cases of the letters in sentence (see examples). You should change the sentence as you go, and do so in one pass from left to right (see examples 4 and 5).


Parameters:String, int[], String[]
Method signature:String fix(String sentence, int[] times, String[] dictionary)
(be sure your method is public)


-The words in dictionary are all lowercase, but applying the transformation should be done without regard to case (see examples)


-dictionary will contain between 1 and 50 elements, inclusive.
-Each element of dictionary will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
-sentence will contain between 1 and 50 characters, inclusive.
-Each character of sentence will be a letter ('a'-'z' or 'A'-'Z') or a space (' ').
-There will be no leading, trailing, or double spaces in sentence.
-times will contain the same number of elements as sentence has characters.
-Each element of times will be between 0 and 1,000,000, inclusive.
-times will be sorted in strictly ascending order.


"Th EProblem"
Returns: "ThE Problem"
The space is typed after 55 milliseconds, while the 'e' is typed after 75. Since these are within 20 milliseconds of each other, they may be swapped.
Returns: "TH EPROBLEM"
Here the 'E' is typed 44 milliseconds after the space and hence may not be swapped.
"TH Eproblem"
Returns: "TH Eproblem"
Though the 'E' and the space are typed within 20 milliseconds of each other, both "th" and "eproblem" are in the dictionary, so no swap occurs.
"onc eupo n atime"
Returns: "onc eupo n atime"
Note that if we applied the transformation at all of the spaces, we would end up with 4 valid words, but our algorithm only does one pair at a time, and hence can do nothing here.
"Th eQuic kBrown Fox JUMPE Dover"
Returns: "The Quick Brown Fox JUMPE Dover"
Originally, we can't swap the space and the 'k' to form "eQuick", because that is not in the dictionary. However, since we are working from left to right, and changing the sentence as we go, we move the 'e' to form "The", (as "quic" is in the dictionary) and then when we look at the 'k', we are allowed to move it.
"a bbb aab a b"
Returns: "a bbba ab a b"
Notice that, after we apply the transformation between the second and third words, if we started over we would apply the transformation between the first and second words. However, we are applying transformations in one pass, from left to right, so we don't go back and do this.
"ab cd ef"
Returns: "abc d ef"

Problem url:

Problem stats url:




schveiguy , vorthys

Problem categories:

String Manipulation