Abbreviation


Problem Statement :


You can perform the following operations on the string, :

1. Capitalize zero or more of a's lowercase letters.
2. Delete all of the remaining lowercase letters in a.

Given two strings, a and b, determine if it's possible to make a equal to b as described. If so, print YES on a new line. Otherwise, print NO.

Function Description

Complete the function abbrevation in the editor below. It must return either YES or NO.

abbreviation has the following parameter(s):

a: the string to modify
b: the string to match
Input Format

The first line contains a single integer q, the number of queries.

Each of the next q pairs of lines is as follows:
- The first line of each query contains a single string, a.
- The second line of each query contains a single string, b.

Constraints

1   <=   q  <=  10
1   <=   | a |, | b |   <=  1000
String a consists only of uppercase and lowercase English letters, ascii[A-Za-z].
String b consists only of uppercase English letters, ascii[A-Z].


Output Format

For each query, print YES on a new line if it's possible to make string a equal to string b. Otherwise, print NO.

Sample Input

1
daBcd
ABC
Sample Output

YES



Solution :



title-img


                            Solution in C :

In    C  :







#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<ctype.h>

int main() {

      
    int q,i,j,n,m;
    char a[1005],s[1005];
    scanf("%d",&q);
    while(q--)
        {
        scanf("%s",s);
        scanf("%s",a);
        n=strlen(s);
        m=strlen(a);
        for(i=0,j=0;i<n && j<m;i++)
            {
                if(s[i]<94)
                    {
                        while(s[i]!=a[j] && j<m)
                            j++;
                        if(j==m)
                            break;
                        else
                        {
                            s[i]=a[j]=91;
                        }
                    }
            }
        if(i!=n && j==m)
            {
            printf("NO\n");
            continue;
            }
        i=0;
        while(s[i]==91 && i<n)
            i++;
        for(j=i;i<n&&j<m;i++)
            {
                if(s[i]==91 && a[j]!=91 )
                    break;
                else if(s[i]==91 && a[j]==91)
                    j++;
                else
                 {
                    if(s[i]==a[j]+32) 
                        j++;
                }
            }
        if(j==m)
            printf("YES\n");
        else
            printf("NO\n");
        
    }
    return 0;
}
                        


                        Solution in C++ :

In   C ++  :






#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <numeric>
#include <functional>

using namespace std;

char a[2048], b[2048];

int main() {
  int T;
  scanf("%d", &T);
  for (int testcase = 1; testcase <= T; testcase++) {
    scanf("%s%s", a, b);
    int n = strlen(a), m = strlen(b);
    vector<vector<int>> dp(n+1, vector<int>(m+1));
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (i == 0 && j == 0) {
          dp[i][j] = 1;
          continue;
        }
        bool ok = false;
        if (i > 0 && j > 0 && toupper(a[i-1]) == b[j-1] && dp[i-1][j-1]) {
          ok = true;
        }
        if (i > 0 && islower(a[i-1]) && dp[i-1][j]) {
          ok = true;
        }
        dp[i][j] = ok ? 1 : 0;
      }
    }
    if (dp[n][m]) {
      printf("YES\n");
    }
    else {
      printf("NO\n");
    }
  }
  return 0;
}
                    


                        Solution in Java :

In   Java  :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        sc.nextLine();
        for (int z = 0; z < q; z++) {
            char[] a = sc.nextLine().toCharArray();
            char[] b = sc.nextLine().toCharArray();
            boolean[][] dp = new boolean[a.length+1][b.length+1];
            for (int i = 0; i <= a.length; i++)
            dp[i][0] = true;
            for (int i = 1; i <= a.length; i++) {
                if (a[i-1]>='A'&&a[i-1]<='Z') {
                    for (int j = 1; j <= b.length; j++) {
                        if (b[j-1]==a[i-1])
                            dp[i][j] = dp[i-1][j-1];
                    }
                } else {
                    char c = (char)(a[i-1]-32);
                    for (int j = 1; j <= b.length; j++) {
                        if (b[j-1]==c)
                            dp[i][j] = dp[i-1][j-1];
                        dp[i][j] |= dp[i-1][j];
                    }
                }
            }
            System.out.println(dp[a.length][b.length]?"YES":"NO");
        }
    }
}
                    


                        Solution in Python : 
                            
In   Python3  :




#!/usr/bin/env python3

def search(needle, haystack):
    if not haystack:
        return False
    if not needle:
        for ch in haystack:
            if ch.isupper():
                return False
        return True
    if needle == haystack.upper():
        return True
    if haystack[0].isupper() and needle[0] != haystack[0]:
        return False
    elif needle[0] == haystack[0]:
        return search(needle[1:], haystack[1:])
    if needle[0] == haystack[0].upper():
        if search(needle[1:], haystack[1:]):
            return True
    return search(needle, haystack[1:])

def main():
    queries = int(input().strip())
    for i in range(queries):
        src = input().strip()
        tgt = input().strip()
        if search(tgt, src):
            print('YES')
        else:
            print('NO')

if __name__ == '__main__':
    main()
                    


View More Similar Problems

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →