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
Answer:
int main()
{
int Q[100],i,j,count=0;
long int N,Qn;
char s[100],ch;
scanf("%ld",&N);
scanf("%s",s);
scanf("%ld",&Qn);
for(i=0;i<Qn;i++)
{
scanf("%d",&Q[i]);
}
for(i=0;i<Qn;i++)
{
ch=s[Q[i]-1];
count=0;
for(j=0;j<Q[i]-1;j++)
{
if(ch==s[j])
count++;
}
printf("%d",count);
}
return 0;}
Explanation:
class solve
{
public static void main(String args[])
{
Scanner sc= new Scanner(System . in);
int arr [ ];
System . out . println("Enter the length of string");
int len=sc . nextInt();
arr =new int [ len ];
System . out . println("Enter the string");
string str=sc . next();
System . out . println("Enter number of queries to be made");
int q=sc . nextInt();
for ( int i = 0; i< q; i++)
{
int count =0;
System . out . println( "Enter the query" );
int p=sc . nextInt();
for (int j=0; j<p-1 ; j++)
{
if( str . charAt ( j ) = = str . charAt ( p-1 ) )
{
count + + ;
}
}
arr [ i ] = count;
}
for ( int i= 0; i< q; i++)
{
System . out . println (arr [ i ]);
}
}
}