Maximum Palindromes

  • + 0 comments

    For me last five test cases are showing run time errors. And remaining are working well Can anyone see and tell the mistake what i have done import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    static String str; static BigInteger[] fact=new BigInteger[101]; static void initialize(String s) { // This function is called once before all queries. str=s; fact[0]=new BigInteger("1"); for(int i=1;i<101;i++) { fact[i]=new BigInteger(""+i); fact[i]=fact[i].multiply(fact[i-1]); // System.out.println(fact[i]); }

    }
    
    static int answerQuery(int l, int r) {
        // Return the answer for this query modulo 1000000007.
        int[] arr=new int[26];
        String sub=str.substring(l-1,r);
        for(int i=0;i<sub.length();i++)
        {
    
            arr[sub.charAt(i)%97]++;
        }
        int odd=0,even=0;
        ArrayList<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<26;i++)
        {
            int te=arr[i]/2;
            if(te>0)
            {
                  even+=te;
            list.add(te);
            }
    
            odd+=arr[i]%2;
        }
     BigInteger big=fact[even];
        for(int i=0;i<list.size();i++)
        {
            big=big.divide(fact[list.get(i)]);
        }
        if(odd>0)
        big=big.multiply(BigInteger.valueOf(odd));
        big=big.mod(BigInteger.valueOf(1000000007));
        String ss=big+"";
    
       return Integer.parseInt(ss); 
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        initialize(s);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            int l = in.nextInt();
            int r = in.nextInt();
            int result = answerQuery(l, r);
            System.out.println(result);
        }
        in.close();
    }
    

    }