One simple solution for leetcode problem 14

First let us figure out what should i do,we need find the common prefix for all strings in the vector

So we just need compare the char in the same position for each string
But how to denote the particular char in the particular string in a vector?


This content originally appeared on DEV Community and was authored by hallowaw

Image description

First let us figure out what should i do,we need find the common prefix for all strings in the vector

So we just need compare the char in the same position for each string
But how to denote the particular char in the particular string in a vector?
Let us use the example 1 for a explanation
So if we wanna denote the 'r' in the 'flower' ,we can use strs[0][5] to refer 'r'
So now we get the methods to make a comparison for each char,if it is the same we save it into the result string
but we also need to notice if it exit in the position of a string
We use the code to check :if(strs[j][i]=='\0')

so the full code is:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        //first we define a string for storing the result
        string commonprefix="";
        int condition=1;
        for(int i=0;i<strs[0].size();i++){
            char currentchar=strs[0][i];

            for(int j=1;j<strs.size();j++)
            {
                //should require two conditions :the position of the string is not NULL and same with currentchar,here we use the contrary condition for understand easily.
                if(strs[j][i]=='\0'||strs[j][i]!=currentchar){
                    return commonprefix;
                }
            }
            commonprefix.push_back(currentchar);
        }
        return commonprefix;
    }
};

Thank you for reading!


This content originally appeared on DEV Community and was authored by hallowaw


Print Share Comment Cite Upload Translate Updates
APA

hallowaw | Sciencx (2024-06-24T14:42:52+00:00) One simple solution for leetcode problem 14. Retrieved from https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/

MLA
" » One simple solution for leetcode problem 14." hallowaw | Sciencx - Monday June 24, 2024, https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/
HARVARD
hallowaw | Sciencx Monday June 24, 2024 » One simple solution for leetcode problem 14., viewed ,<https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/>
VANCOUVER
hallowaw | Sciencx - » One simple solution for leetcode problem 14. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/
CHICAGO
" » One simple solution for leetcode problem 14." hallowaw | Sciencx - Accessed . https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/
IEEE
" » One simple solution for leetcode problem 14." hallowaw | Sciencx [Online]. Available: https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/. [Accessed: ]
rf:citation
» One simple solution for leetcode problem 14 | hallowaw | Sciencx | https://www.scien.cx/2024/06/24/one-simple-solution-for-leetcode-problem-14/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.