A Problem on String
Practice
3.7 (752 votes)
Hiring
Approved
Very Easy
Hash function
Problem
34% Success 9984 Attempts 10 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given a string S of length N consisting of only lower-case English alphabets, you will be asked to process Q queries over it . In each query you will be given two lower case characters X and Y. Your task is to find out the number of such substrings of the the string S which have the characters X and Y on either of its end points, both X...Y and Y...X are considered to be valid.

Note : Substrings length should be greater than 1.

Input:
The first line of the input will contain N , the length of the string.
Next line will contain as string of length N. Next line will will contain Q , the number of queries. Then Q subsequent lines will contain two lowercase characters X and Y separated by a space.

Output:
For each query , output the answer in a separate line.

Constraints:

  • \(1 \le N \le 10^6\)
  • \(1 \le Q \le 10^3\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
CombinatoricsMathematicsMediumOpenApproved
Points:20
155 votes
Tags:
MathematicsLucky-DigitEasySimple-math
Points:30
8 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen