Skip to content

Commit 19a92c4

Browse files
Append and Delete Problem
1 parent f6a0fb1 commit 19a92c4

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

append_and_delete.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
You have a string of lowercase English alphabetic letters. You can perform two types of operations on the string:
3+
4+
Append a lowercase English alphabetic letter to the end of the string.
5+
Delete the last character in the string. Performing this operation on an empty string results in an empty string.
6+
Given an integer, , and two strings, and , determine whether or not you can convert to by performing exactly of the above operations on . If it's possible, print Yes. Otherwise, print No.
7+
8+
For example, strings and . Our number of moves, . To convert to , we first delete all of the characters in moves. Next we add each of the characters of in order. On the move, you will have the matching string. If there had been more moves available, they could have been eliminated by performing multiple deletions on an empty string. If there were fewer than moves, we would not have succeeded in creating the new string.
9+
10+
Function Description
11+
12+
Complete the appendAndDelete function in the editor below. It should return a string, either Yes or No.
13+
14+
appendAndDelete has the following parameter(s):
15+
16+
s: the initial string
17+
t: the desired string
18+
k: an integer that represents the number of operations
19+
Input Format
20+
21+
The first line contains a string , the initial string.
22+
The second line contains a string , the desired final string.
23+
The third line contains an integer , the number of operations.
24+
25+
Constraints
26+
27+
and consist of lowercase English alphabetic letters, .
28+
Output Format
29+
30+
Print Yes if you can obtain string by performing exactly operations on . Otherwise, print No.
31+
32+
Sample Input 0
33+
34+
hackerhappy
35+
hackerrank
36+
9
37+
Sample Output 0
38+
39+
Yes
40+
Explanation 0
41+
42+
We perform delete operations to reduce string to hacker. Next, we perform append operations (i.e., r, a, n, and k), to get hackerrank. Because we were able to convert to by performing exactly operations, we print Yes.
43+
44+
Sample Input 1
45+
46+
aba
47+
aba
48+
7
49+
Sample Output 1
50+
51+
Yes
52+
Explanation 1
53+
54+
We perform delete operations to reduce string to the empty string (recall that, though the string will be empty after deletions, we can still perform a delete operation on an empty string to get the empty string). Next, we perform append operations (i.e., a, b, and a). Because we were able to convert to by performing exactly operations, we print Yes.
55+
56+
Sample Input 2
57+
58+
ashley
59+
ash
60+
2
61+
Sample Output 2
62+
63+
No
64+
Explanation 2
65+
66+
To convert ashley to ash a minimum of steps are needed. Hence we print No as answer.
67+
*/
68+
69+
#include <bits/stdc++.h>
70+
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
71+
#define f(i,a,b) for(i=a; i<b; i++)
72+
#define fr(i,a,b) for(i=a; i>=b; i--)
73+
#define endl '\n'
74+
#define ll long long int
75+
#define ff(x) first(x)
76+
#define ss(x) second(x)
77+
#define pb(x) push_back(x)
78+
#define mp make_pair
79+
#define mod 1000000007
80+
#define sz(x) ((int) (x).size())
81+
82+
using namespace std;
83+
84+
int main()
85+
{
86+
string s, t;
87+
int k;
88+
cin>>s>>t>>k;
89+
int p = 0;
90+
while (p < min(sz(s), sz(t)) && s[p] == t[p])
91+
++p;
92+
int vmin;
93+
if (k % 2 == (sz(s) + sz(t)) % 2)
94+
vmin = sz(s) + sz(t) - 2 * p;
95+
else
96+
vmin = sz(s) + sz(t) + 1;
97+
if (k < vmin)
98+
cout << "No\n";
99+
else
100+
cout << "Yes\n";
101+
}

0 commit comments

Comments
 (0)