Find longest common prefix?

前端 未结 9 1854
忘掉有多难
忘掉有多难 2020-12-10 03:50

In two strings:

\"Mary Had a Little Lamb\"
\"Mary Had a Big Lamb\"

should return

\"Mary Had a \"

9条回答
  •  醉梦人生
    2020-12-10 04:02

    Use binary search. Try to compare whole strings. If they are not equal try to compare the first characters. If they are equal try to split the strings (substring(0, str.length()/2). Etc, etc.

提交回复
热议问题