Write a function to find the longest common prefix string amongst an array of strings.
[]
= ""
["Abcweed", "Htgdabc", "SABCRF"]
= ""
["Abcweed", "ABHTGDC", "ABACRF"]
= "AB"
To find the oldest prefix of a string array
1 Public classsolution{2 PublicString Longestcommonprefix (string[] strs) {3 //System.out.println (arrays.tostring (STRs));4 if(Strs.length = = 0)5 return"";6 intMin =Integer.max_value;7 for(String s:strs) {8 if(min>s.length ())9Min =s.length ();Ten } One if(min==0) A return""; - //System.out.println (min); -String prefix = "", TMP = ""; the intI=0; - for(; i<min; i++){ -prefix = strs[0].substring (0, (i+1)); - //System.out.println ("prefix=" + prefix); + for(intK=p; j<strs.length; J + +){ -TMP = strs[j].substring (0, (i+1)); + //System.out.println ("tmp=" + tmp); A if(!prefix.equals (TMP)) at returnStrs[0].substring (0, (i)); - } - } -System.out.println ("i=" + strs[0].substring (0, (i))); - return""; - } in - to Public Static voidMain (string[] args) { +String[] arr = {"", "ASDFFG", "ASWD"}; - if(args.length!=0) thearr =args; *Solution solution =Newsolution (); $String res =Solution.longestcommonprefix (arr);Panax Notoginseng System.out.println (res); - } the}
[Leetcode]-011-longest Common Prefix