# Short Palindrome

### Problem Statement :

Consider a string, , of  lowercase English letters where each character,  (, denotes the letter at index  in . We define an  palindromic tuple of  to be a sequence of indices in  satisfying the following criteria:

, meaning the characters located at indices  and  are the same.
, meaning the characters located at indices  and  are the same.
, meaning that , , , and  are ascending in value and are valid indices within string .
Given , find and print the number of  tuples satisfying the above conditions. As this value can be quite large, print it modulo .

unction Description
Complete the function shortPalindrome in the editor below.

shortPalindrome has the following paramter(s):
- string s: a string

Returns
- int: the number of tuples, modulo

Input Format

A single string, .

Constraints

It is guaranteed that  only contains lowercase English letters.

### Solution :

Solution in C :

In   C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

char s[1000000] = "hello";
int n = 0;
long long* map[26];
int maxLen = 0;
long long* work;
long long* psum;
long long* ssum;
long long* rsum;
const long long modul = 1000000007;

long long calc(int len);
long long* getArr(char c);
void solve(long long* c1, long long* c2);
long long solveOne(long long l);

int main(int argc, char* argv[]) {
char c = ' ';
for (int i = 0; i <= 1000005; ++i) {
c = getchar();
if (('\n' == c) || (EOF == c)) {
s[i] = '\0';
n = i;
break;
} else {
s[i] = c - 'a';
}
}

for (char c = 0; c < 26; ++c) {
map[c] = getArr(c);
//printf("%lld\n", map[c][0]);
if (map[c][0] > maxLen)
maxLen = map[c][0];
}

work = malloc((maxLen + 1) * sizeof(long long));
psum = malloc((maxLen + 1) * sizeof(long long));
ssum = malloc((maxLen + 1) * sizeof(long long));
rsum = malloc((maxLen + 1) * sizeof(long long));

long long ans = 0;
for (char c1 = 0; c1 < 26; ++c1) {
for (char c2 = 0; c2 < 26; ++c2) {
if (c1 == c2) {
if (map[c1][0] > 0) {
//printf("%d\n", map[c1][0]);
long long t = solveOne(map[c1][0]);
ans = (ans + t) % modul;
}
} else {
if ((map[c1][0] > 0) && (map[c2][0] > 0)) {
solve(map[c1], map[c2]);
//for (int i = 0; i < map[c1][0] + 1; ++i) printf("%d ", work[i]);
//printf("\n");
ans += calc(map[c1][0] + 1);
ans = ans % modul;
}
}
}
}

printf("%lld\n", ans);
return 0;
}

long long solveOne(long long l) {
long long res = 0;
for (long long i = 1; i <= (l - 3); ++i) {
res += (i * (l - 2 - i) * (l - 1 - i) / 2) % modul;
res = res % modul;
}
res = res % modul;
return res;
}

long long calc(int len) {
psum[0] = work[0];
//printf("psum: %d ", psum[0]);
for (int i = 1; i < len; ++i) {
psum[i] = psum[i - 1] + work[i];
//printf("%d ", psum[i]);
}
ssum[0] = psum[0];
//printf("\nssum: %d ", ssum[0]);
for (int i = 1; i < len; ++i) {
ssum[i] = (ssum[i - 1] + psum[i]) % modul;
//printf("%d ", ssum[i]);
}
rsum[len - 1] = work[len - 1];
for (int i = len - 2; i >= 0; --i) {
rsum[i] = rsum[i + 1] + work[i];
}
/*(printf("\nrsum: ");
for (int i = 0; i < len; ++i) {
printf("%d ", rsum[i]);
}
printf("\n");*/
long long res = 0;
for (int i = 2; i < len; ++i) {
res += (rsum[i] * ssum[i - 2]) % modul;
}
res = res % modul;
return res;
}

void solve(long long* c1, long long* c2) {
long long l1 = 1;
long long l2 = 1;
long long r = 0;
long long count = 0;
for (;;) {
if ((l1 > c1[0]) && (l2 > c2[0])) {
work[r] = count;
++r;
break;
} else if (l1 > c1[0]) {
++l2;
++count;
} else if (l2 > c2[0]) {
++l1;
work[r] = count;
count = 0;
++r;
} else if (c1[l1] > c2[l2]) {
++l2;
++count;
} else if (c1[l1] < c2[l2]) {
++l1;
work[r] = count;
count = 0;
++r;
}
}
}

long long* getArr(char c) {
long long count = 0;
for (long long i = 0; i < n; ++i)
if (c == s[i])
++count;

long long *res = malloc(sizeof(long long) * (count + 1));
long long cur = 1;
res[0] = count;
for (long long i = 0; i < n; ++i)
if (c == s[i])
res[cur++] = i;

return res;
}

Solution in C++ :

In  C++  :

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define fre 	freopen("0.in","r",stdin);freopen("0.out","w",stdout)
#define abs(x) ((x)>0?(x):-(x))
#define MOD 1000000007
#define LL signed long long int
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define rep(i,from,to) for(int i=(from);i <= (to); ++i)
#define pii pair<int,int>

vector<int> G[2*100000+5];
LL suf[654];
LL pre[654];
LL dp[29][29];

int main(){
//fre;
string S;
cin>>S;
int N = S.size();
LL ans = 0;
S = " " + S;
for(int i=1;i<=N;++i)suf[S[i]-'a'+1]++;
for(int i=1;i<=N;++i){
int x = S[i]-'a'+1;
suf[x]--;
for(int j=1;j<=26;++j){
ans += dp[j][x] * suf[j];
ans %= MOD;
}
for(int j=1;j<=26;++j){
dp[j][x] += pre[j];
}
pre[x]++;
}
cout<<ans;
}

Solution in Java :

In   Java  :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) throws Exception{
String s = inR.readLine().trim();
long MOD = 1000000000L+7;
long[] cnt = new long[26];
long[][] sumK = new long[26][26];
long[][] delta = new long[26][26];
long ans = 0;
for(int i=0; i<s.length(); i++) {
int c = s.charAt(i) - 'a';
for(int j=0; j<26; j++) {
ans = (ans + delta[c][j]) % MOD;
}

for(int j=0; j<26; j++) {
delta[j][c] = (delta[j][c] + sumK[j][c]) % MOD;
sumK[j][c] = (sumK[j][c] + cnt[j]) % MOD;
}

cnt[c] += 1;
}
System.out.println(ans % MOD);
inR.close();
}
}

Solution in Python :

In   Python3 :

#!/bin/python3

import math
import os
import random
import re
import sys
import collections

# Complete the shortPalindrome function below.
def shortPalindrome(s):
arr1 = [0]*26
arr2 = [[0]*26 for i in range(26)]
arr3 = [0]*26
ans = 0
for i in range(len(s)):
idx = ord(s[i]) - ord('a')
ans += arr3[idx]
for j in range(26):
arr3[j] += arr2[j][idx]
for j in range(26):
arr2[j][idx] += arr1[j]
arr1[idx] += 1
return ans % (10**9+7)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

s = input()

result = shortPalindrome(s)

fptr.write(str(result) + '\n')

fptr.close()

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

## Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

## Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

## Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

## Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

## Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty