dongtan8532 2012-03-20 17:25 采纳率: 0%
浏览 47
已采纳

确定现有字符串的所有子字符串的最快方法

Let's say I have the string "Hey". I would like to determine all combinations of characters that exist in this string as fast as possible. The resulting algorithm should generate this:

H, e, y, He, ey, Hey

The algorithm should not produce the string "Hy" because it does not exist in the string as a substring.

  • 写回答

2条回答 默认 最新

  • dongyu9263 2012-03-20 17:37
    关注

    There are O(n^2) of those substrings, of lengths [1,n], so any algorithm to generate all of them will be O(n^2) * O(n) = O(n^3):

    (*) See Edit2 at the end - depending on the implementation of the string - the complexity can vary from O(n^2) to O(n^3)

    Pseudo code:

    result <- {} #result is a set if dupes should be terminated, otherwise - it is a multiset.
    for i from 0 to s.length:
       for j from i+1 to s.length:
          result.add(s.substring(i,j))
    return result
    

    Note however, that you can do some "cheating", by creating an iterator and generate the substrings on the fly, it should look something like this [pseudo code]:

    class MyIterator:
      String s
      int i,j
      MyIterator(String s):
         this.s = s
         i = 0
         j = 0
      next():
         j = j + 1
         if (j >= s.length):
         i = i + 1
         j = i + 1
         if (i >= s.length): 
             throw exception
         return s.substring(i,j)
    

    Note that creating the iterator is O(1), and each iteration is O(n) - but to actually produce all the elements, you need O(n^2) steps, so complexity remains O(n^3) overall, but you decrease the latency of your application.

    EDIT:
    I editted complexity, claiming it is O(n^2) is wrong, the complexity is O(n^3) since you need to generate strings of variable lengths, some of them are long. At least half of the generated substrings will be of length n/2 - thus the total complexity is Theta(n^3)

    EDIT2:
    In some cases it can actually be O(n^2) - depending on the string implementation. In java for example - it uses a single char[], and only "plays" with the offset and length - so in java - creation is actually O(n^2), since creating a substring is O(1)
    In C however - it is O(n^3), since every substring needs to be copied to a different char[].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?