• 문제 링크
Longest Common Prefix - LeetCode
Can you solve this real interview question? Longest Common Prefix - Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow"
leetcode.com
• 풀이 코드
public class Solution {
public String longestCommonPrefix(String[] strs) {
int min = 200;
for (String s : strs) min = Math.min(min, s.length());
for (int i = 0; i < min; i++)
for (int j = 1; j < strs.length; j++)
if (strs[0].charAt(i) != strs[j].charAt(i)) return strs[0].substring(0, i);
return strs[0].substring(0, min);
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 21. Merge Two Sorted Lists - Java (0) | 2026.05.13 |
|---|---|
| [LeetCode] 20. Valid Parentheses - Java (0) | 2026.05.12 |
| [LeetCode] 13. Roman to Integer - Java (0) | 2026.05.10 |
| [LeetCode] 12. Integer to Roman - Java (0) | 2026.05.09 |
| [LeetCode] 11. Container With Most Water - Java (0) | 2026.05.08 |
댓글