Leetcode: longest common prefix

Source: Internet
Author: User

Question Link

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

The meaning of the question is not very clear, and it is understood as the elders of any two string prefixes. However, this question is to find the longest common prefix of all strings, that is, all strings in the array contain this prefix.

Algorithm 1: Character-by-character comparison. The time complexity is O (n * l). N indicates the number of strings and L indicates the length of the longest prefix.

Class solution {public: String longestcommonprefix (vector <string> & STRs) {int n = STRs. size (); string res; If (n = 0) return res; For (INT Pos = 0; POS <STRs [0]. size (); POS ++) // The maximum prefix length cannot exceed STRs [0]. size (), character-by-character comparison {for (int K = 1; k <n; k ++) // The POS character of STRs [0] And STRs [1... n-1 if (STRs [K]. size () = POS | STRs [k] [POS]! = STRs [0] [POS]) return res;} res. push_back (STRs [0] [POS]) ;}return res ;}};

======================================

Algorithm 2: 0th strings and other strings are prefixed one by one. The longest prefix length of all strings = min {prefixlength (STRs [0], STRs [I]), 0 <I <n} address of this Article

Class solution {public: String longestcommonprefix (vector <string> & STRs) {int n = STRs. size (); If (n = 0) Return ""; int Len = STRs [0]. size (); // The length of the longest prefix for (INT I = 1; I <n; I ++) {int K; For (k = 0; k <min (Len, (INT) STRs [I]. size (); k ++) if (STRs [0] [k]! = STRs [I] [k]) break; If (LEN> K) Len = K;} return STRs [0]. substr (0, Len );}};

 

The time complexity of algorithm 2 is O (n * m), n is the number of strings, M = STRs [0] and the average length of the prefix of other strings, the number of characters in algorithm 2 is more than that in algorithm 1. Example:

 

STRs [0]: abcdef

STRs [1: ABCDE

STRs [2: abcdk

STRs [3] AB

According to algorithm 1, only the first three characters of each string need to be compared; according to algorithm 2, STRs [0] and the other three strings need to be compared 6, 5, 3 Characters

 

[Copyright statement] reprinted please indicate the source: http://www.cnblogs.com/TenosDoIt/p/3856331.html

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.