India Languages, asked by varaprasadakula999, 11 months ago

Tahir and Mamta are woking in a project in TCS. Tahir being a problem solver came up with an interesting problem for his friend Mamta.
Problem consists of a string of length N and contains only small case alphabets.
It will be followed by Q queries, in which each query will contain an integer P (1<=P<=N) denoting a position within the string.
Mamta's task is to find the alphabet present at that location and determine the number of occurrence of same alphabet preceding the given location P.
Mamta is busy with her office work. Therefore, she asked you to help her.
Constraints
1 <= N <= 500000
S consisting of small case alphabets
1 <= Q <= 10000
1 <= P <= N
Input Format
First line contains an integer N, denoting the length of string.
Second line contains string S itself consists of small case alphabets only ('a' - 'z').
Third line contains an integer Q denoting number of queries that will be asked.
Next Q lines contains an integer P (1 <= P <= N) for which you need to find the number occurrence of character present at the Pth location preceeding P.

Answers

Answered by poojan
0

Python program to "Similar Char Problem".

Language used : Python Programming

Program :

n=int(input("enter string length :"))

stri=list(str(input("Enter the string :"))[:n])

nq=int(input("Enter the number of queries :"))

for i in range(nq):

 count=0

 q= int(input("Enter query :"))

 alpha = stri[q-1]

 for j in range(0,q-1):

     if alpha==stri[j]:

         count=count+1

 print("count is :",count)

Input :

enter string length :9

Enter the string :abacsddaa

Enter the number of queries :2

Enter query :9

Enter query :3

Output :

count is : 3

count is : 1

Explanation :

  • Firstly, take the string length to be inputted from the user.

  • Take string as an input from the user of the specified length mentioned, and list the elements in the string.

  • Ask user about the number of queries he is going to perform.

  • Run a loop ranging the number of queries to be made, and on entering the loop, ask user to input the query.

  • Now, write an inner for loop ranging from 0 to the query -1 index.

  • Now, check the value present in the given queryindex with the one's before them and keep on incrementing the count every time the same element found.

  • Once done, come out of the innerloop andd print the count, and then, go for next query, if asked.

  • Keep on repeating the same process until all the queries are done, outer loop terminates. That's it!

NOTE :

If you don't want descriptive statements in the input and outputs, delete them from the corresponding statements in the code.

Learn more :

1) Printing all the palindromes formed by a palindrome word.

brainly.in/question/19151384

2) Indentation is must in python. Know more about it at :

brainly.in/question/17731168

3) Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

brainly.in/question/15473120

4) Python program to find absolute difference between the odd and even numbers in the inputted number.

brainly.in/question/11611140

Similar questions