Computer Science, asked by ashwinsrinivas6, 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.

Answers

Answered by sudee7936
1

Answer:

using System;

public class Program

{

public static void Main()

{

int len=int.Parse(System.Console.ReadLine());

string s=System.Console.ReadLine();

int  q = int.Parse(Console.ReadLine());

for(int i=0;i<q;i++){

int p=int.Parse(System.Console.ReadLine());

string temp=s.Substring(0,p-1);

System.Console.WriteLine(count(temp,s[p-1]));

}

}

static int count(string s,char c){

int count=0;

foreach(char k in s){

if(k==c){

count++;

}

}

return(count);

}

}

Explanation:

Answered by sahilshm11
0

Answer:

n=int(input())

s=input()

q=int(input())

for i in range(q):

    p=int(input())

    print(s.count(s[p-1],0,p-1))

Explanation:

count function will count the number of occurrence's of the required char which is found using "s[p-1]" , "0" for starting index and p-1 for the length till the required count.

Similar questions