Longest Common PrefixTotal accepted:44093 Total submissions:169565my submissions QuestionSolution
Write a function to find the longest common prefix string amongst an array of strings.
Hide TagsStringHas you met this question in a real interview? Yes No
Discuss
This is a simple question that finds the longest prefix string in a series of strings
#include <iostream> #include <string> #include <vector>using namespace std;string Longestcommonprefix (vector<string>& strs) {string result;if (Strs.empty ()) Return result;if (Strs.size () ==1 {Result=strs[0];return result;} int len1=strs[0].size (); for (int j=0;j<len1;++j) {Result.push_back ((strs[0]) [j]), int len2=strs.size (); for (int i=0 ; i<len2;++i) {int len3=strs[i].size (); if (J>=len3) {result.pop_back (); return result;} if ((Strs[0]) [j]!= (Strs[i]) [j]) {result.pop_back (); return result;}}} return result;} int main () {vector<string> str;str.push_back ("asdf"), Str.push_back ("Asdgk"), Str.push_back ("ASD");cout< <longestcommonprefix (str) <<endl;}
Leetcode_14 topic--longest Common Prefix (String)