Cerinta completa
Madam Hannah Otto, the CEO of Reviver Corp., is fond of palindromes, or words that read the same forwards or backwards. She thinks palindromic brand names are appealing to millennials.
As part of the marketing campaign for the company’s new juicer called the Rotator™, Hannah decided to push the marketing team’s palindrome-searching skills to a new level with a new challenge.
In this challenge, Hannah provides a string consisting of lowercase English letters. Every day, for days, she would select two integers and , take the substring (the substring of from index to index ), and ask the following question:
Consider all the palindromes that can be constructed from some of the letters from . You can reorder the letters as you need. Some of these palindromes have the maximum length among all these palindromes. How many maximum-length palindromes are there?
For example, if , and , then we have,

Your job as the head of the marketing team is to answer all the queries. Since the answers can be very large, you are only required to find the answer modulo .
Complete the functions initialize and answerQuery and return the number of maximum-length palindromes modulo .
Input Format
The first line contains the string .
The second line contains a single integer .
The of the next lines contains two space-separated integers , denoting the and values Anna selected on the day.
Constraints
Here, denotes the length of .
Subtasks
For 30% of the total score:
For 60% of the total score:
Output Format
For each query, print a single line containing a single integer denoting the answer.
Sample Input 0
week
2
1 4
2 3
Sample Output 0
2
1
Explanation 0
On the first day, and . The maximum-length palindromes are “ewe” and “eke”.
On the second day, and . The maximum-length palindrome is “ee”.

Sample Input 1
abab
1
1 4
Sample Output 1
2
Explanation 1
Here, the maximum-length palindromes are “abba” and “baab”.
Limbajul de programare folosit: java8
Cod:
import java.io.*;
import java.util.*;
public class Solution {
static final long MOD = 1_000_000_007L;
private static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { this.in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
String next() throws IOException {
int c;
do { c = read(); } while (c <= ' ' && c != -1);
if (c == -1) return null;
StringBuilder sb = new StringBuilder();
while (c > ' ') {
sb.append((char) c);
c = read();
}
return sb.toString();
}
int nextInt() throws IOException { return Integer.parseInt(next()); }
}
static long modPow(long a, long e) {
long r = 1;
while (e > 0) {
if ((e & 1) == 1) r = (r * a) % MOD;
a = (a * a) % MOD;
e >>= 1;
}
return r;
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
String s = fs.next();
int n = s.length();
int[][] pref = new int[26][n + 1];
for (int i = 0; i < n; i++) {
int ch = s.charAt(i) - 'a';
for (int c = 0; c < 26; c++) pref[c][i + 1] = pref[c][i];
pref[ch][i + 1]++;
}
long[] fact = new long[n + 1];
long[] invFact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % MOD;
invFact[n] = modPow(fact[n], MOD - 2);
for (int i = n; i > 0; i--) invFact[i - 1] = (invFact[i] * i) % MOD;
int q = fs.nextInt();
StringBuilder out = new StringBuilder();
while (q-- > 0) {
int l = fs.nextInt();
int r = fs.nextInt();
int half = 0;
int odd = 0;
long denomInv = 1;
for (int c = 0; c < 26; c++) {
int cnt = pref[c][r] - pref[c][l - 1];
if ((cnt & 1) == 1) odd++;
int h = cnt >> 1;
half += h;
denomInv = (denomInv * invFact[h]) % MOD;
}
long ways = fact[half];
ways = (ways * denomInv) % MOD;
if (odd == 0) odd = 1;
ways = (ways * odd) % MOD;
out.append(ways).append('\n');
}
System.out.print(out);
}
}
Scor obtinut: 1.0
Submission ID: 464616094
Link challenge: https://www.hackerrank.com/challenges/maximum-palindromes/problem
