🐍Python/Python_알고리즘

[알고리즘] 41. Append and Delete

728x90
반응형

You have a string of lowercase English alphabetic letters. You can perform two types of operations on the string:

  1. Append a lowercase English alphabetic letter to the end of the string.
  2. 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.

Constraints

  •  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., ran, 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., ab, 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.


답  : 



#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the appendAndDelete function below.
if __name__ == '__main__':
s = input()
t = input()
k = int(input())
n = min(len(s),len(t))
idx = n
for i in range(n):
if s[i] != t[i]:
idx = i
break
# 겹치는 부분 2번 빼줘서 뒷 부분만 남도록
needs = len(s) + len(t) - 2*idx


if k==needs or (k >= needs and (k-needs) % 2==0) or k >= len(s)+len(t):
print('Yes')
else:
print('No')


먼저 어느 index부터 달라지는지 값을 저장해 두었다. 그리고 무조건 바뀌어야하는 이동의 수를 구하기 위해 배열의 숫자가 달라지는 전부분들을 다 날렸다.


그 이후에 짝수로 나뉘어야하는 이유는 2의 배수 이동이어야 +-0이 되기 때문이다. 그리고 배열의 합 자체가 k보다 작으면 가능하다고 판단했다. 

728x90
반응형