Maximum Palindromes

Sort by

recency

|

21 Discussions

|

  • + 0 comments

    1.can anybody tell why m i failing with some test cases.

      -
    #include <iostream>
    #include <memory.h>
    #include <vector>
    #define mod % 1000000007
    using namespace std;
    #define ll long long
    ll int cal(vector<vector<ll int>>  &dp,vector<ll int> &fact,ll int l,ll int r)
    {
        
        ll int len=0;
        ll int odd=0;
        vector<ll int> v(26,0);
        for(int i=0;i<26;i++)
        { len += (dp[r][i]-dp[l-1][i])/2;
          //cout<<"\n len = "<<len;
           v[i]=(dp[r][i]-dp[l-1][i])/2;
           if((dp[r][i]-dp[l-1][i])%2)
              odd++;
        }
         ll int ans=fact[len];
         ll int d=1;
        for(int i=0;i<26;i++)
        {d*=fact[v[i]] % 1000000007; 
         d= d mod;
        }
         ans = (ans*max((ll int)1,odd))mod;
         d= ans/d;
        return d%1000000007;
    }
    int main() {
         string s;
         cin>>s;
         vector<vector<ll int>>  dp;
         dp.resize(s.size() + 1);
         for(ll int i=0;i<=s.size();i++)
         dp[i].resize(26);  
         
         for(int i=0 ; i < s.size();i++)
             dp[i+1][s[i]-'a']++;
         
         for(ll int i=0;i<26;i++)
         for(ll int j=1;j<=s.size();j++)
            dp[j][i]+=dp[j-1][i]; 
          
        vector<ll int > fact(100001,0);
        fact[0]=1;
        for(int i=1;i<=100000;i++)
            fact[i]=(fact[i-1]*i)mod;
        ll int q;
        cin>>q;
        while(q--)
        {
            ll int l,r;
            cin>>l>>r;
            cout<<cal(dp,fact,l,r)<<endl;
            
        }
        
    }
    
  • + 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();
    }
    

    }

  • + 0 comments
    for (int i = 0; i <= n; i++) {
            for (int j = 0; j < A; j++) {
                cnt[i][j] += cnt[i - 1][j];
            }
        }
    

    cnt[i][j] += cnt[i - 1][j]; in this line what happen when i=0; cnt[0][j]+=cnt[-1][j] as it has a index -1 does it make any sence

    it is a editorial solution

  • + 0 comments

    could someone tell me, why do we need inverses of factorial...also what is mistake in my code..pls. Code in GFG IDE

  • + 0 comments

    https://www.youtube.com/watch?v=cJsNNcQlSPA https://youtu.be/_bRVA5b4sb4

    This might help understand the editorial.