Saturday 13 December 2014

Given three strings s1, s2 and s3 as input, return true if there exists at least one character that is present in all 3 strings s1, s2 and s3. presentIn3("trisect", "learn", "code") = true (as e is present is all) presentIn3("Google", "Apple", "Microsoft") = false (as no character is present is all)

public class PresentIn3 {
    static String testcase1="sunil";
    static String testcase2="anil";
    static String testcase3="bidhu";
   
    public static void main(String args[]){
        PresentIn3 testInstance=new PresentIn3();
        boolean result=testInstance.findPresenceOfchar(testcase1,testcase2,testcase3);
        System.out.println(result);
       
    }
    public boolean findPresenceOfchar(String str1, String str2,String str3){
        char temp=' ';
        boolean caseIs=false;
        String foundMax="";
        String foundThisChar="";
        int i=0,countAtStr2=0,countAtStr3=0;
        int lenOfStr2=0,lenOfStr3=0;
        while(str1.length()>0){
            System.out.println("str1 length  issss"+str1.length());
            temp=str1.charAt(i);
            foundThisChar=str1.valueOf(temp);
            System.out.println("foundThisChar issssss"+foundThisChar);
            for(int j=0;j<str2.length();j++){
                System.out.println("jjjjjjjjjjjj"+j);
                if(str2.regionMatches(j, foundThisChar,0,foundThisChar.length())){
                    countAtStr2++;
                    System.out.println("countAtStr2 issssss"+countAtStr2);
                }
            }
            for(int l=0;l<str3.length();l++){
                System.out.println("l issssssss"+l);
                if(str3.regionMatches(l, foundThisChar,0,foundThisChar.length())){
                    countAtStr3++;
                    System.out.println("countAtStr3 isssssss"+countAtStr3);
                }
            }
           
           str1= str1.replace(foundThisChar,"");
            str1=str1.replaceAll("\\s+","");
            System.out.println("str1 isssssss::::"+str1);
            if(countAtStr2==0&&countAtStr3==0)
                caseIs=false;
            else if(countAtStr2>=1&&countAtStr3>=1){
                foundMax=foundMax+temp;
                caseIs=true;
                System.out.println(caseIs);
                break;
            }
            else
                caseIs=false;
        countAtStr2=0;
        countAtStr3=0;
        }
    return caseIs;
    }
   
   
}
 

No comments:

Post a Comment