class Solution { public: string longestCommonPrefix(vector<string>& strs) { int n1=strs.size(); int ans=strs[0].length(); for(int i=1;i<n1;i++) { int j=0; while(j<strs[i].size() && strs[i][j]==strs[0][j]) { j++; } ans=min(ans,j); } return strs[0].substr(0,ans); } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter