Problem Solving

2-3. Longest Common Prefix (LeetCode 14)

tls1107 2021. 7. 7. 01:23
728x90
반응형

 


 

 class Solution {
    public String longestCommonPrefix(String[] strs) {
        String answer = "";
        char tmp;
        for(int i=0 ; i<strs[0].length() ; i++){
            tmp = strs[0].charAt(i); 
            for(int j=0 ; j<strs.length ; j++){
                if(i >= strs[j].length()) return answer;
                if(tmp != strs[j].charAt(i) ) return answer;     
            }
        answer += tmp;
        }
        return answer;
    }
}

https://leetcode.com/problems/longest-common-prefix/

 

Longest Common Prefix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

728x90
반응형