Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 925 Bytes

40.md

File metadata and controls

46 lines (37 loc) · 925 Bytes

METHOD 1( better than 33% )

 bool find(string s)
    {
        int n=s.size();
        for(int i=0;i<n/2;i++)
            if(s[i]!=s[n-i-1])
                return 0;
        return 1;
    }
    string firstPalindrome(vector<string>& words) {
        

        for(auto str: words)
            if(find(str))
                return str;

        return "";
    }

Method 2 (better than 87%)

 bool find(string s)
    {
        int i=0,j=s.size()-1;
        while(j>i)
        {
            if(s[i++]!=s[j--])
                return 0;
        }
        return 1;
    }
    string firstPalindrome(vector<string>& words) {
        

        for(auto str: words)
            if(find(str))
                return str;

        return "";
    }