finding common prefix of array of strings

后端 未结 17 1705
一向
一向 2020-11-29 06:15

I have an array like this:

$sports = array(
\'Softball - Counties\',
\'Softball - Eastern\',
\'Softball - North Harbour\',
\'Softball - South\',
\'Softball -         


        
17条回答
  •  感情败类
    2020-11-29 06:54

    I would use a recursive algorithm like this:

    1 - get the first string in the array 2 - call the recursive prefix method with the first string as a param 3 - if prefix is empty return no prefix 4 - loop through all the strings in the array 4.1 - if any of the strings does not start with the prefix 4.1.1 - call recursive prefix method with prefix - 1 as a param 4.2 return prefix

提交回复
热议问题