-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppendAndDeleteHR.py
87 lines (64 loc) · 2.67 KB
/
AppendAndDeleteHR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
You have a string of lowercase English alphabetic letters. You can perform two types of operations on the string:
Append a lowercase English alphabetic letter to the end of the string.
Delete the last character in the string. Performing this operation on an empty string results in an empty string.
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.
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.
Function Description
Complete the appendAndDelete function in the editor below. It should return a string, either Yes or No.
appendAndDelete has the following parameter(s):
s: the initial string
t: the desired string
k: an integer that represents the number of operations
Input Format
The first line contains a string , the initial string.
The second line contains a string , the desired final string.
The third line contains an integer , the number of operations.
and consist of lowercase English alphabetic letters, .
Output Format
Print Yes if you can obtain string by performing exactly operations on . Otherwise, print No.
Sample Input 0
hackerhappy
hackerrank
9
Sample Output 0
Yes
Explanation 0
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.
Sample Input 1
aba
aba
7
Sample Output 1
Yes
Explanation 1
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.
Sample Input 2
ashley
ash
2
Sample Output 2
No
Explanation 2
To convert ashley to ash a minimum of steps are needed. Hence we print No as answer.
"""
# SOLUTION
c = 0
s = input()
t = input()
k = int(input())
l = len(s)
while s[:l]!=t[:l]:
l-=1
c+=1
o = ((len(t)-l)+c)
if k<o:
print("No")
elif (len(s)+len(t))<=k:
print("Yes")
elif 2*len(t)<k:
print("Yes")
elif k%2 == o%2:
print("Yes")
else:
print("No")