Why this code is not working!


#1

Can someone explain me why this code is not working for large inputs:

public class Solution {
public int findRank(String A) {
int len=A.length();
long sum=1;
HashSet set=new HashSet();
ArrayList a=new ArrayList();

    for(int i=0;i<len;i++){
        a.add(A.charAt(i));
        set.add(A.charAt(i));
    }
    
    Collections.sort(a);
    
    for(int i=0;i<len-1;i++){
        char c=A.charAt(i);
        
        int index=0;
        
        while(c!=a.get(index)){
            index++;
        }
        
        if(set.size()>0){
            sum=sum+((long)(index)*fact(set.size()-1));
        }else{
            //sum++;
        }
        set.remove(c);
        a.remove(index);
    }
    
    return (int)(sum%1000003); 
}

static long fact(int n){
    if(n==0){
        return 1;
    }
    
    return n*fact(n-1);
}

}
Blog