Given an array of strings (terms), find the number of occurrences of each term in a string. You need only fill out the Count method.
Parameters:
- Use any data structures available to you, including ones you create.
- The term count map must be ordered in the order of the terms string array (for example,
fizzfirst,buzzsecond,fizzbuzzthird). - Duplicate terms in the array are not allowed (assume a process has previously de-duped the terms in the array).
- Cannot use any regular expression API/libraries.
- Blank strings (i.e.
"") andnullshould be handled and returned with all terms set to zero. - No exceptions should be thrown or need to be handled.
Return a map of each term to its occurrence count in the input string.
这道题要求你在不使用正则表达式的前提下,统计输入字符串中每个 term 出现的次数,并且返回结果时必须保持 terms 数组的原始顺序。最直接的做法是遍历 terms,用逐字符匹配或双指针方式统计每个词在字符串中的出现次数,再把结果放进有序映射结构(如 LinkedHashMap)中。需要特别处理空字符串和 null:这两种情况下应直接返回所有 term 的计数为 0。
正文完