FastPrepNumber Words to Digits in Any Language

Number Words to Digits in Any Language

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW

Problem statement

A number is written in words, in English, Chinese, Japanese, or another language. Convert it to its value.

The words are already split, in order, in words. The arrays vocabulary and values give their meaning: the word vocabulary[i] has the value values[i]. That mapping is all you get. Your code must not need a rule about digit groups for each language, such as four digits for Chinese 万 and 亿 or three digits for English thousand and million.

Read the words from left to right, keeping a list of amounts:

  • A word with value 0, such as 零, changes nothing.
  • Otherwise, if the amounts at the end of the list are smaller than the word's value, the word multiplies them: replace all of those smaller amounts with their sum times the value.
  • If the last amount is not smaller, or the list is empty, the word starts a new amount equal to its value.

Return the sum of the amounts left in the list. It is 0 when every word has the value 0.

Function

wordsToNumber(words: String[], vocabulary: String[], values: long[]) → long

Examples

Example 1

words = ["two","hundred","thirty","four","thousand","five","hundred","six"]vocabulary = ["two","four","five","six","thirty","hundred","thousand"]values = [2,4,5,6,30,100,1000]return = 234506

two starts the amount 2, and hundred multiplies it to 200. thirty and four are each smaller than the amount before them, so the list becomes 200, 30, 4.

thousand is larger than all three, so it multiplies their sum: 234 * 1000 = 234000. Then five hundred makes 500 and six makes 6.

The sum is 234000 + 500 + 6 = 234506.

Example 2

words = ["一","亿","二","千","三","百","万","四","千","零","五"]vocabulary = ["零","一","二","三","四","五","百","千","万","亿"]values = [0,1,2,3,4,5,100,1000,10000,100000000]return = 123004005

一亿 makes 100000000. 二千 makes 2000 and 三百 makes 300. 万 multiplies both, but not the larger first amount: 2300 * 10000 = 23000000.

四千 makes 4000. 零 changes nothing, and 五 starts the amount 5.

The sum is 100000000 + 23000000 + 4000 + 5 = 123004005.

Example 3

words = ["三","千","五","百","万"]vocabulary = ["三","五","百","千","万"]values = [3,5,100,1000,10000]return = 35000000

This is Japanese. 三千 makes 3000 and 五百 makes 500. 万 multiplies both: 3500 * 10000 = 35000000.

Constraints

  • 1 <= words.length <= 34
  • 1 <= vocabulary.length <= 64
  • values.length == vocabulary.length
  • 1 <= words[j].length <= 12
  • 1 <= vocabulary[i].length <= 12
  • 0 <= values[i] <= 10^15
  • The entries of vocabulary are all different, and every word in words is in vocabulary.
  • words spells a correct number in its language, no larger than 10^15, and the rules above give its real value. After each word, every amount kept so far is larger than the amount after it.

More Microsoft problems

See Microsoft hiring insights
Saved
public long wordsToNumber(String[] words, String[] vocabulary, long[] values) {
    // Write your code here.
}
Checking account…