这是一个问题,但这是一个简单的javascript版本:它对数组进行排序,然后仅查看第一个和最后一个项目。
//数组中最长的公共起始子字符串
function sharedStart(array){ var A= array.concat().sort(), a1= A[0], a2= A[A.length-1], L= a1.length, i= 0; while(i<L && a1.charAt(i)=== a2.charAt(i)) i++; return a1.substring(0, i);}德莫斯
sharedStart(['interspecies', 'interstelar', 'interstate']) //=> 'inters'sharedStart(['throne', 'throne']) //=> 'throne'sharedStart(['throne', 'dungeon']) //=> ''sharedStart(['cheese']) //=> 'cheese'sharedStart([]) //=> ''sharedStart(['prefix', 'suffix']) //=> ''


![在一组字符串中找到最长的公共起始子字符串[关闭] 在一组字符串中找到最长的公共起始子字符串[关闭]](http://www.mshxw.com/aiimages/31/617409.png)
