Append and Delete

Append and Delete

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, k, and two strings, s and t, determine whether or not you can convert s to t by performing exactly k of the above operations on s. If it’s possible, print Yes. Otherwise, print No.

For example, strings s = [a,b,c] and t = [d,e,f]. Our number of moves, k = 6. To convert s to t, we first delete all of the characters in 3 moves. Next we add each of the characters of t in order. On the 6th 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 6 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 s, the initial string.

The second line contains a string t, the desired final string.

The third line contains an integer k, the number of operations.

ConstraintstaumAndBday.md

  • 1 <= |s| <= 100
  • 1 <= |t| <= 100
  • 1 <= k <= 100
  • s and t consisit of lowercase English alphabetic letters, ascii[a-z].

Output Format

Print Yes if you can obtain string t by performing exactly k operations on s. Otherwise, print No.

Sample Input 0

1
2
3
hackerhappy
hackerrank
9

Sample Output 0

1
Yes

Explanation 0

We perform 5 delete operations to reduce string s to hacker. Next, we perform 4 append operations (i.e., r, a, n, and k), to get hackerrank. Because we were able to convert s to t by performing exactly k = 9 operations, we print Yes.

Sample Input 1

1
2
3
aba
aba
7

Sample Output 1

1
Yes

Explanation 1

We perform 4 delete operations to reduce string s to the empty string (recall that, though the string will be empty after 3 deletions, we can still perform a delete operation on an empty string to get the empty string). Next, we perform 3 append operations (i.e., a, b, and a). Because we were able to convert s to t by performing exactly k = 7 operations, we print Yes.

Sample Input 2

1
2
3
ashley
ash
2

Sample Output 2

1
No

Explanation 2

To convert ashley to ash a minimum of 3 steps are needed. Hence we print No as answer.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Complete the appendAndDelete function below.
function appendAndDelete(s, t, k) {
let ready = true;
let total = s.length + t.length;
let count = new Array(Math.min(t.length, s.length)).fill(0).reduce((target, item, index) => {
(ready && Object.is([...s][index], [...t][index]))
? target++
: (ready = false);

return target;
}, 0);

return ((total - 2 * count) % 2 == k % 2) || (total <= k)
? (((total - 2 * count) > k) ? 'No' : 'Yes')
: 'No';
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×