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 he
Answers
Answer:
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 ]);
}
}
}
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