knakul853

Untitled

Jul 20th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string longestCommonPrefix(vector<string>& strs) { // binary search can be used.
  4.        
  5.         int n = strs.size();
  6.        
  7.         int ans = 1;
  8.         if(strs.empty())return "";
  9.        
  10.        
  11.         for(int ans = 1;true;ans++){
  12.        
  13.         for(string s:strs){
  14.            
  15.             if(s.length() < ans || s[ans-1]!=strs[0][ans-1]){
  16.                 return s.substr(0,ans-1);
  17.             }
  18.         }
  19.        
  20.         }
  21.     }
  22. };
Add Comment
Please, Sign In to add comment