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

Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

View Solution →

Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →