본문 바로가기
Problem Solving

2-3. Longest Common Prefix (LeetCode 14)

by tls1107 2021. 7. 7.
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
반응형

'Problem Solving' 카테고리의 다른 글

2-5. 단어 공부 (백준 1157)  (0) 2021.07.07
2-4. 다이얼 (백준 5622)  (0) 2021.07.07
2-2. OX퀴즈 (백준 8958)  (0) 2021.07.07
2-1. 비밀번호 발음하기 (백준 4659)  (0) 2021.07.07
1-10. 농구 경기 (백준 1159)  (0) 2021.07.07

댓글