Computer Science, asked by arjun9752, 1 year ago

pleae give me code any programming langauge................................Similar Char
Problem Description
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.

Output
For each query, print an integer denoting the answer on single line.

Test Case

Explanation
Example 1

Input

9

abacsddaa

2

9

3

Output

3

1

Explanation

Here Q = 2

For P=9, character at 9th location is 'a'. Number of occurrences of 'a' before P i.e., 9 is three.

Similarly for P=3, 3rd character is 'a'. Number of occurrences of 'a' before P. i.e., 3 is one.

Answers

Answered by dalalrohit102
9

Answer:

n=int(input()) #length of string

s=input() #string

q=int(input()) #test cases

p=[]

for i in range(0,q):

   p.append(int(input()))

   

#loop through cases

for i in range(0,q):

   a=p[i] #entry of p array    

   a=a-1

   c=0

   for x in range(0,a):

       if(s[x]==s[a]):

           #print(s[x])

           c=c+1

   print(c)

Explanation:

CodeVita compiler is giving TLE error. Succesfully running on local machine

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 output, delete them from the input and output 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.

https://brainly.in/question/11611140

Attachments:
Similar questions