Leetcode longest Common Prefix longest prefix

Source: Internet
Author: User
Tags first string

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

Translation: asks for a common longest prefix in a string array.

Train of thought: Take the first string as the benchmark, traverse the position by location, and iterate through the string array, if a string length is smaller than the current position, or the character of the current position is different, return string strs[0].substring (0,pos);

Code:

    public string Longestcommonprefix (string[] strs) {      int count = strs.length;//The number of strings     int pos = 0;//current pointer position     if ( Count = = 0| | Strs[0].length () ==0)//judgment null     return "";     For (Pos < Strs[0].length ();p os++)     {for     (int j = 1; j < count;j++)     {     if (strs[j].length () <=pos || Strs[0].charat (POS)!=strs[j].charat (POS))//over length, or unequal     return strs[0].substring (0, POS);   return string     }     }     return strs[0];    }

Leetcode longest Common Prefix longest prefix

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.