Computer Science, asked by sivarudra910, 1 year 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 ItzShWeTlAnA3026
4

❗❕Seems like your question lacks some vital details plz clarify your question first ❗❕

Answered by sailorking
1

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

}

}

}

Similar questions