Problem · String

Case-Preserving Dictionary Autocorrect

Learn this problem
EasyBlock logoBlockFULLTIMEPHONE SCREEN

Problem statement

You are given a case-sensitive dictionary of allowed words and one input word.

  • If word exactly matches an allowed word, return it unchanged.
  • Otherwise, if a dictionary word is equal to word when both are converted to lowercase, return the dictionary word with its stored casing.
  • If there is no case-insensitive match, return word unchanged.

No two dictionary entries have the same lowercase representation, so the case-insensitive correction is unique.

Function

autocorrect(dictionary: String[], word: String) → String

Examples

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 dictionary is at most 9500.
drafts saved locally
public String autocorrect(String[] dictionary, String word) {
    // write your code here
}
dictionary["Cats","dogs","GhOSt"]
word"CaTS"
expected"Cats"
checking account