Hey i am getting error while submitting on leetcode

please anyone help me out
class Solution {
public:
string longestCommonPrefix(vector& strs) {

string ans=strs[0];
if(strs.size()==0)
{
    string s="";
    return s;
}
for(int i=0;i<strs.size();i++)
{
    string temp="";
    string cmp=strs[i];
    for(int j=0;j<min(ans.length(),cmp.length());j++)
    {
        if(ans[j]==cmp[j])
        {
            temp+=ans[j];
        }
        else
        {
            break;
        }
    }
    ans=temp;
}
return ans;
}

};
Line 924: Char 9: runtime error: reference binding to null pointer of type ‘std::__cxx11::basic_string<char, std::char_traits, std::allocator >’ (stl_vector.h)this error i am getting…

@shivammishra20121999, share the code using some ide due to formatting some parts may be missing

i didnot get it wht you are trying to say

i didnot get it what you are trying to say

https://codeshare.io/GA9JYe

@shivammishra20121999, you are not handling the case when the strs vector is empty , as in that case executing this line string ans=strs[0]; will raise a runtime error

add these lines at starting of your solution :-

if(strs.size()==0){
    return "";
}

corrected code :-

what i was saying is when sending your code use online ide like i have used (https://ide.codingblocks.com/) , it makes it easier for us to understand your code and reply faster, as code in chats are formatted and sometimes changes the code ,which makes it harder for us to run the code in our computer

In case of anytime feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

@shivammishra20121999 Abhijeet is right!
you should first check whether strs is empty, and then access strs[0], as if it is empty, then strs[0] throws runtime error,
hence your code should be
class Solution {
public:
string longestCommonPrefix(vector& strs) {

if(strs.size()==0)
{
    string s="";
    return s;
}
string ans=strs[0];
for(int i=0;i<strs.size();i++)
{
    string temp="";
    string cmp=strs[i];
    for(int j=0;j<min(ans.length(),cmp.length());j++)
    {
        if(ans[j]==cmp[j])
        {
            temp+=ans[j];
        }
        else
        {
            break;
        }
    }
    ans=temp;
}
return ans;
}

};

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.