Leetcode [14] (Java): Longest Common Prefix

Source: Internet
Author: User
Tags stringbuffer

title : Longest common prefix

Difficulty : Easy

topic content :

Write a function to find the longest common prefix string amongst an array of strings.

If There is no common prefix, return an empty string "" .

Translate : Write a function to find the longest common prefix string in a string array.

If there is no public prefix, an empty string is returned.

Example 1:

Input: ["Flower", "flow", "Flight"]output: "FL"

Example 2:

Input: ["dog", "racecar", "Car"]output: "" Explanation:there is no common prefix among the input strings.

Note:

All inputs are lowercase letters A-Z.

My idea : The simplest way to traverse the shortest length of string[] is to take the current position character of each of the string[] in the traversal, and return the result if it's not the same as the next comparison.

Another is that, with set, in the internal traversal with set will string[] each of the current position characters in the time to determine whether the size () is ==1, OK, this space complexity is higher, it seems more stupid.

1      PublicString Longestcommonprefix (string[] strs) {2         if(Strs.length < 1) {3             return"";4         }5         6         intMinlen =Integer.max_value;7          for(inti = 0;i < strs.length; i++) {8             if(Strs[i].length () <Minlen) {9Minlen =strs[i].length ();Ten             } One}//get the shortest length Minlen A          -          -StringBuffer SB =NewStringBuffer (); the          for(intj = 0; J < Minlen; J + +) { -              for(inti = 0; i< strs.length-1; i++) { -                 if(Strs[i].charat (j)! = Strs[i+1].charat (j)) { -                     returnsb.tostring (); +                 } -             } +Sb.append (strs[0].charat (j)); A         } at         returnsb.tostring (); -}

The complexity of my time : O (n*m) N is the number of strings, M shortest string length

Coding Process Issues :
1, a start directly take strs[0] the length of the loop, resulting in test case {aa,a} out of Bounds, written Minlen after

2, the initial value of Minlen was first written in Min_value.

Reference Answer code:

1      PublicString Longestcommonprefix (string[] strs) {2         if(STRs = =NULL|| Strs.length = = 0)3             return"";4String pre = Strs[0];5          for(inti = 1; i < strs.length; i++)6              while(Strs[i].indexof (PRE)! = 0) 7Pre = pre.substring (0, Pre.length ()-1);8         returnPre;9}

9 Lines! It's amazing.

complexity of the answer: O (n*m) Although the complexity looks the same, but in fact, each while loop does not have a cycle M, but also less to take Minlen operation

The answer : Take any string to do the initial prefix, and then for each of the following a while loop: if the current prefix with your string match result is not 0 "is 0 is the prefix, NULL, and other characters do not", then the last character of the pre is removed, then match, Until it is 0.

Because it is to take the longest public prefix, so it is similar to the wooden bucket short board effect, the final value will be the shortest, so the pre is saved to the next and then a while loop.

Leetcode [14] (Java): Longest Common Prefix

Related Article

Contact Us

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.