Free Puzzle Questions with Answer using Java, C#, JavaScript and PHP

Java Quiz - Find Largest Common Prefix

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findlargestcommonprefix;

/**
 *
 * @author Bereket-Excellerent
 */
public class FindLargestCommonPrefix {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String[] input = {"c","c"};
        
        String result = longestCommonPrefix(input);
        
        System.out.println(result);
    }
    
    public static String longestCommonPrefix(String[] givenArray){
        if(givenArray.length == 0){
            return "";
        }
        else{
            String firstString = givenArray[0];
            String foundCharacters = "";
            
            if(firstString.length() == 0){
                return "";
            }
            else{
                int counter = 0;
                for(int i=0; i < firstString.length(); i++){
                    foundCharacters = firstString.substring(0, i+1);
                    
                    for(int j=1; j < givenArray.length; j++){
                        if(givenArray[j].length() == 0){
                            return "";
                        }
                        else if(givenArray[j].length() >= foundCharacters.length()){
                            String current = givenArray[j].substring(0, i+1);
                            
                            if(foundCharacters.equals(current) == false){
                                return firstString.substring(0, i);
                            }
                        }
                        else{
                            return firstString.substring(0, i);
                        }
                    }
                    counter++;
                }
                
                return firstString.substring(0, counter);
            }
        }
    }
    
}