Wednesday, February 9, 2022

Longest Common Prefix

function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
       
       
       

class Solution {
   public static void main(String[] args) {
    System.out.println("Should be (fl). ="+new Solution().longestCommonPrefix(new String[]{"flower","flow","flight"}));
    System.out.println("Should be (""). ="+new Solution().longestCommonPrefix(new String[]{"box","wow","cat"})); 
  }
    
    public String longestCommonPrefix(String[] strs) {
        String firstString = strs[0];
        String commonPrefix="";
      boolean common=true;
        for(int j=0; j

No comments: