PHP (5) "Longest common prefix" algorithm problem

Source: Internet
Author: User
The original topic is this: Write a function to find the longest common prefix in a string array. Returns an empty string "" If there is no public prefix

Example 1:
Input: ["Flower", "flow", "flight"]
Output: "FL"
Example 2:
Input: ["dog", "racecar", "car"]
Output: ""
Explanation: The input does not exist with a public prefix.
Description
All inputs contain only lowercase letters A-Z.

```//code show as below
Class test
{
Public function a6(\$arr)
{
/ / Get the first value in the string array, based on the value of this element, the elements after the array are compared with this
\$common_str= \$arr[0];
/ / Traverse the array, cut each string into an array, after cutting is such an array (0 => array (), 1 => array () ... n => array ()), you can put The result is considered to be a matrix. The number of rows in the matrix is how many strings, that is, the subscripts of the incoming array. The number of columns is the index of the array after each string is cut.
Foreach (\$arr as \$key=>\$value)
{
\$arr[\$key] = str_split(\$value);
}
//here, \$arr has become a two-dimensional array
\$length = count(\$arr);
\$temp = \$arr[0];
//Because we want to compare each string to the first one of the incoming array, we take the first one as a base value.
\$len = count(\$temp);
//The \$i in this for loop is the column of the matrix, so that the ith bit of each substring array can be fetched.
For (\$i=0;\$i<\$len;\$i++)
{
//The \$n in the for loop is the row of the matrix, which is the subscript of the incoming string array.
For (\$n=1; \$n<\$length; \$n++)
{
/ / If the ith bit of the base value is not equal to the i-th bit of the following string, intercept the base string to the ith bit
If(\$temp[\$i]!=\$arr[\$n][\$i])
{
// Of course, there is a special case here, that is, when comparing the 0th bit to the end, it will be off, then it will return without the same prefix.
If(\$i == 0)
{
Return "no identical prefix";
}
Return substr(\$common_str,0,\$i);
}
}
/ / Here the judgment is to add the base string traversal completed, all the same, then we return the base string
If (\$i==\$len-1)
{
Return \$commen_str;
}
}
}
}
```

PHP (5) "Longest common prefix" algorithm problem

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.