Skip to content

Latest commit

 

History

History
34 lines (33 loc) · 809 Bytes

DAY2P1.md

File metadata and controls

34 lines (33 loc) · 809 Bytes

Longest Common Prefix


  • Question:

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:

Input: strs = ["flower","flow","flight"]

Output: "fl"


  • Solution:

Code :

class Solution {
   public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0)
            return "";
        
        Arrays.sort(strs);
        String first = strs[0];
        String last = strs[strs.length - 1];
        int c = 0;
        while(c < first.length())
        {
            if (first.charAt(c) == last.charAt(c))
                c++;
            else
                break;
        }
        return c == 0 ? "" : first.substring(0, c);
    }
}