String Similarity

  • + 0 comments

    //Java solution using recursion function(fails for the test case 11 and 12 yet i got 85.04):

    public static int stringSimilarity(String s){

    int count=s.length();
    for (int i = 1; i < s.length(); i++) {
    
            if(s.charAt(0)==s.charAt(i) && i<s.length()-1) 
            {count+=1+fidd(s,i+1,1,0);}
    
            else if(s.charAt(0)==s.charAt(i) && i==s.length()-1){
                count+=1;
            }
    }
    return count;
    }
    
    public static int fidd(String s ,int k ,int i ,int count){
        if(s.charAt(i)==s.charAt(k) && k<s.length()-1) {
            count=1+fidd(s, k+1, i+1, count);
        }
        else if(s.charAt(i)==s.charAt(k) && k==s.length()-1){count=1;}
    
        else count=0;
    
        return count;
    
    }