i18n
InterviewTime limit1sMemory limit128 MB
Given text lines, expand i18n-style abbreviations back into previously seen full words when the expansion is valid and unique, preserving capitalization and separators.
- Level
Medium5 of 10
- Topics
- String, Hash map, Implementation
- Solved
- No attempts yet
Problem
Internationalization and localization are long words that are commonly abbreviated as i18n and l10n. The number between the first and last letters is the count of letters that were omitted. This is a powerful abbreviation scheme that can be applied naturally to any word.
A word may be abbreviated only if it has already appeared earlier in the text. To abbreviate a word, keep only its first and last letters and replace every letter in between with the count of omitted letters.
Your task is to write a program that expands such abbreviations in a given text wherever possible. An abbreviation can be expanded only if the expansion is both valid and unambiguous.
- An expansion is valid if the full word has appeared earlier in the text and its abbreviated form matches the abbreviation being expanded. Case is ignored when checking validity. For example, expanding
i18ntointernationalizationis valid (even ifinternationalizationpreviously appeared only with a capitalI). Expandingp14ntoparameterizationis not valid, becauseparameterizationnever appeared before the abbreviation. Expandinga11ntoabbreviationis not valid, becausea11nis not the correct abbreviation ofabbreviation(the correct one isa10n). - An expansion is unambiguous if exactly one valid expansion exists. For example,
l10nunambiguously expands tolocalization, butp5mcannot be expanded unambiguously, because bothproblemandprogramabbreviate top5m.
Input
The input contains at most 1000 lines, each with at most 80 characters. Each line contains one or more words separated by spaces and the special symbols -, ,, ., ", (, ), :, ;, !, ?. There are no trailing spaces, but a line may end with one of the other separators.
Each word is either full or abbreviated.
- A full word consists of 1 to 32 Latin letters and appears in one of three capitalization forms: all lowercase, First capital letter, or ALL CAPITAL LETTERS.
- An abbreviated word consists of a Latin letter, followed by a number from 2 to 30 (without a leading zero), followed by a Latin letter. Abbreviated words use the three corresponding capitalization forms: all
l7e,F3t C5l L4r, or ALLC5L L5S.
Output
Reproduce the original text with its original separators, expanding abbreviated words into full words wherever possible (as described above). The capitalization of an expanded word must match the capitalization of the abbreviation it replaces.
Leave invalid or ambiguous abbreviations unchanged (still abbreviated). Note that lines in the output may be longer than 80 characters.