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 :
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
Sparse Arrays
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun
View Solution →Array Manipulation
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu
View Solution →Print the Elements of a Linked List
This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode
View Solution →Insert a Node at the Tail of a Linked List
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink
View Solution →Insert a Node at the head of a Linked List
Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below
View Solution →Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →