Problem · String
Case-Preserving Dictionary Autocorrect
Learn this problemProblem statement
You are given a case-sensitive dictionary of allowed words and one input word.
- If
wordexactly matches an allowed word, return it unchanged. - Otherwise, if a dictionary word is equal to
wordwhen both are converted to lowercase, return the dictionary word with its stored casing. - If there is no case-insensitive match, return
wordunchanged.
No two dictionary entries have the same lowercase representation, so the case-insensitive correction is unique.
Function
autocorrect(dictionary: String[], word: String) → StringExamples
Example 1
dictionary = ["Cats","dogs","GhOSt"]word = "CaTS"return = "Cats"CaTS is not an exact dictionary entry, but its lowercase form matches Cats.
Example 2
dictionary = ["Cats","dogs"]word = "dogs"return = "dogs"The exact, case-sensitive dictionary match is returned immediately.
Example 3
dictionary = ["Cats","dogs"]word = "cat"return = "cat"No dictionary entry equals cat, even after lowercasing, so the input is left unchanged.
Constraints
1 <= dictionary.length <= 1000.1 <= dictionary[i].length, word.length <= 100.- Every word contains only uppercase or lowercase English letters.
- Dictionary entries are distinct.
- No two dictionary entries have the same lowercase representation.
- The total number of characters in
dictionaryis at most9500.