Search for strings matching the pattern “abc:*:xyz” in less than O(n)

前端 未结 4 1058
有刺的猬
有刺的猬 2021-01-07 10:45

Given a bunch of strings I need to find those which match 3 kinds of patterns:

  • Prefix search - abc*
  • Glob-like pattern - abc:*:xyz
  • Suffix sear
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-07 11:16

    If you take the possibility of storing the strings in a search tree into account, why not also store the properties "starts with abc" and "ends with xyz", using these properties as keys?

    Edit: You also might want to leave Big-O-Notation behind and rather concentrate on the actual expected search duration in your particular use case. That's probably a more realistic approach; O(f(n)) style grades for your algorithm / implementation probably don't give you much usable information when it comes to your real-life search efficiency.

提交回复
热议问题