longestCommonPrefix finds the longest common prefix in the string slice.
(strs []string)
| 464 | |
| 465 | // longestCommonPrefix finds the longest common prefix in the string slice. |
| 466 | func longestCommonPrefix(strs []string) string { |
| 467 | if len(strs) == 0 { |
| 468 | return "" |
| 469 | } else if len(strs) == 1 { |
| 470 | return strs[0] |
| 471 | } |
| 472 | |
| 473 | // find out the min/max value by alphabetical order |
| 474 | min, max := strs[0], strs[0] |
| 475 | for _, str := range strs[1:] { |
| 476 | if min > str { |
| 477 | min = str |
| 478 | } |
| 479 | if max < str { |
| 480 | max = str |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // find out the common part between min and max |
| 485 | for i := 0; i < len(min) && i < len(max); i++ { |
| 486 | if min[i] != max[i] { |
| 487 | return min[:i] |
| 488 | } |
| 489 | } |
| 490 | return min |
| 491 | } |
| 492 | |
| 493 | // copyOptions copies the options. |
| 494 | func copyOptions(opts []string) []string { |
no outgoing calls
searching dependent graphs…