Skip to main content

Google kickstart 2022 round A | Speed typing problem sloution

 problem statement : 

Barbara is a speed typer. In order to check her typing speed, she performs a speed test. She is given a string I that she is supposed to type.


While Barbara is typing, she may make some mistakes, such as pressing the wrong key. As her typing speed is important to her, she does not want to spend additional time correcting the mistakes, so she continues to type with the errors until she finishes the speed test. After she finishes the speed test, she produces a P.


Now she wonders how many extra letters she needs to delete in order to get I from P. It is possible that Barbara made a mistake and P cannot be converted back to I just by deleting some letters. In particular, it is possible that Barbara missed some letters.


Help Barbara find out how many extra letters she needs to remove in order to obtain I or if I cannot be obtained from P by removing letters then output IMPOSSIBLE.


Input

The first line of the input gives the number of test cases, T. T test cases follow.


Each test case has 2 lines. The first line of each test case is an input string I (that denotes the string that the typing test has provided). The next line is the produced string P (that Barbara has entered).


Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of extra letters that need to be removed in order to obtain I. If it is not possible to obtain I then output IMPOSSIBLE as y.


Limits

Memory limit: 1 GB.

1≤T≤100.

Both the strings contain letters from a-z and A-Z.

Length of the given strings will be 1≤|I|,|P|≤105.

Test Set 1

Time limit: 20 seconds.

All letters in I are the same.


Test Set 2

Time limit: 40 seconds.

Sample

Note: there are additional samples that are not run on submissions down below.

Sample Input

save_alt

content_copy

2

aaaa

aaaaa

bbbbb

bbbbc

Sample Output

save_alt

content_copy

Case #1: 1

Case #2: IMPOSSIBLE

In the first test case, P contains one extra a, so she needs to remove 1 extra letter in order to obtain I.

In the second test case, Barbara typed only 4 letters b, while I consists of 5 letters b so the answer is IMPOSSIBLE.



Additional Sample - Test Set 2

The following additional sample fits the limits of Test Set 2. It will not be run against your submitted solutions.

Sample Input

save_alt

content_copy

2

Ilovecoding

IIllovecoding

KickstartIsFun

kkickstartiisfun

Sample Output

save_alt

content_copy

Case #1: 2

Case #2: IMPOSSIBLE

In the first test case, P has 2 extra letters, I and l. The other letters are in the order given in I. So she needs to remove 2 letters in order to obtain I.

In the second test case, there is no letter K in P so the answer is IMPOSSIBLE.


solution code :

#include<bits/stdc++.h>
using namespace std;
bool subsequence(string str1 , string str2 , int m , int n)
{    
      int j = 0;
    for(int i = 0 ; i < m  ; i++)
    {  
        int flag =0 ;
        while(flag != 1 )
        {    if(j > n)
             {
                 return false ;
             }
            if(str1[i] == str2[j])
            {
                flag =1;
            }
            j++;
        }
        
    }
    return true ;
}
int main()
{
    cin.tie();
    int t;
    cin >> t;
     
    for(int k =1 ; k <= t ; k++)
    { 
      string p , i;
      cin >> i;
      cin >> p;
      bool x = subsequence(i , p , i.length() , p.length());
        if(x == true)
        {   int len = p.length() - i.length();
          cout << "Case #" << k << ": " <<  len<< "\n" ;
          
        }
        else
        {
          cout << "Case #" << k << ": IMPOSSIBLE" << "\n" ;
        }
        
    }

}

refrences :

Comments

Popular posts from this blog

[PDF DOWNLOAD] AKTU Quantum series data structure b.tech 2nd year download

  All AKTU Quantums are available here. Get your hands on AKTU Quantums and boost your grades in AKTU semester exams. You can either search them category wise or can use the search bar or can manually search on this page. Download aktu second year quantum pdf data structures  download  data structures quantum aktu download aktu data structures quantum click here to download  write in comment section if you want quantum of any other subject.

2485. Find the Pivot Integer | Binary search

  Given a positive integer   n , find the   pivot integer   x   such that: The sum of all elements between  1  and  x  inclusively equals the sum of all elements between  x  and  n  inclusively. Return  the pivot integer  x . If no such integer exists, return  -1 . It is guaranteed that there will be at most one pivot index for the given input.   Example 1: Input: n = 8 Output: 6 Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21. Example 2: Input: n = 1 Output: 1 Explanation: 1 is the pivot integer since: 1 = 1. Example 3: Input: n = 4 Output: -1 Explanation: It can be proved that no such integer exist.   Constraints: 1 <= n <= 1000 Solution : class Solution { publ ic:     int pivotInteger( int n ) {         int sum = (( n )*( n + 1 ))/ 2 ;         int i = 1 ;         int j =...

leetcode 48 solution

  48 .  Rotate Image You are given an  n x n  2D  matrix  representing an image, rotate the image by  90  degrees (clockwise). You have to rotate the image  in-place , which means you have to modify the input 2D matrix directly.  DO NOT  allocate another 2D matrix and do the rotation.   Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]   Constraints: n == matrix.length == matrix[i].length 1 <= n <= 20 -1000 <= matrix[i][j] <= 1000 solution: class Solution { public:     void swap(int& a , int &b)     {         int c ;         c = a;         a = b;         b = c;     }     void transpose (vector<vector<int>...