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.
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 ]);
}
}
}