Title Description:
Write a function to find the longest common prefix string amongst an array of strings.
is to give a 1 string array to find the public longest prefix.
The idea is straightforward, using 1 indexes to save the length of the longest public prefix.
Note that if you use 1 string variables to save the prefix, it is not AC, because the topic does not allow the use of extra space.
public string Longestcommonprefix (string[] strs) { if (STRs = = NULL | | STRs. Length = = 0) { return string. Empty;} if (STRs. Length = = 1) { return strs[0];} var index = 0;for (var j = 0;j < Strs[0]. Length; J + +) {for (var i = 1;i < STRs. Length; i++) {if (J >= Strs[i]. Length | | STRS[0][J]! = Strs[i][j]) {return strs[0]. Substring (0,index);}} index++;} Return strs[0]. Substring (0, index); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode--to find the longest common prefix in a string array