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 :



title-img


                            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{
BufferedReader inR = new BufferedReader(new InputStreamReader(System.in));
        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()
                    


View More Similar Problems

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →