Oracle 面试题:统计字符串中各术语出现次数(禁止正则)— Oracle 面试题 / 文本解析

81次阅读
没有评论

Problem statement:
Given an array of strings (terms) and a target string, find the number of occurrences of each term in the target. You only need to implement the Count method.

Parameters / Requirements:

  • You may use any data structures you create.
  • The returned map/list must follow the same order as the terms array (e.g.,“fizz”first,“buzz”second,“fizzbuzz”third).
  • Assume the terms array has been de-duplicated.
  • Do not use any regular expression APIs/libraries.
  • Blank strings (“”) and null input should be handled; return zeros for all terms.
  • No exceptions need to be thrown/handled.
  • Example test strings include:
    • “fizz buzz fizzbuzz”→ {fizz=2, buzz=2, fizzbuzz=1}
    • “fizzfizz fizz buzz fizzbuzz”→ {fizz=4, buzz=1, fizzbuzz=0}
    • more mixed cases as provided.

禁止正则时,可用 朴素匹配 Aho–Corasick 多模式匹配。面试实现常用:

  • 预处理 terms(可按长度或首字符建索引),遍历目标串,对每个位置尝试匹配可能的 term;命中则计数并继续滑动(是否允许重叠按题意默认允许)。
  • 结果按 terms 原顺序输出;空串 / 空输入统一返回全 0。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Oracle、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0