-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppenDelete.js
More file actions
73 lines (56 loc) · 1.52 KB
/
AppenDelete.js
File metadata and controls
73 lines (56 loc) · 1.52 KB
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
function appendAndDelete(s, t, k) {
let s_copy = Array.from(s);
const t_copy = t;
let counter = 0;
for (let i = 0 ; i< s.length; i++){
console.log(s[i], t[i]);
if(s[i] !== t[i])
{
while( i < s.length )
{
s_copy.pop();
counter++;
i++;
}
console.log(s_copy,counter);
}
}
if(s.length === t.length && counter === 0){ // change all
return (k > 1 && k%2 == 0 )|| k >= t.length *2 ? 'Yes' : 'No';
}
if(s_copy.length === t.length){ //
return k > counter ? 'Yes' : 'No';
}
if(t.length > s_copy.length)
{
let charRequired = t.length - s_copy.length;
if(counter === 0){
return k >= 2 + charRequired && k % charRequired ==0 ? 'Yes' : 'No';
}
return k >= counter + charRequired ? 'Yes' : 'No';
}
}
let stringA = "aba";
let stringB = "aba";
let moves = 7;
console.log(appendAndDelete(stringA, stringB, moves),"Yes"); // Yes
stringA = "abcd";
stringB = "abcdert";
moves = 10;
console.log(appendAndDelete(stringA, stringB, moves),"No"); // No
stringA = "y";
stringB = "yu";
moves = 2;
console.log(appendAndDelete(stringA, stringB, moves),"No"); // No
stringA = "ashley";
stringB = "ash";
moves = 2;
console.log(appendAndDelete(stringA, stringB, moves),"No"); // No
stringA = "hackerhappy";
stringB = "hackerrank";
moves = 9;
console.log(appendAndDelete(stringA, stringB, moves),"Yes"); // Yes
stringA = "abcdef";
stringB = "fedcba";
moves = 15;
console.log(appendAndDelete(stringA, stringB, moves),"Yes"); // Yes