-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppendAndDelete.js
More file actions
66 lines (51 loc) · 1.4 KB
/
AppendAndDelete.js
File metadata and controls
66 lines (51 loc) · 1.4 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
function appendAndDelete(s, t, k) {
let len_s = s.length;
let len_t = t.length;
let len_total = len_s + len_t;
let idx = 0
if (len_total % 2 != 0 && k % 2 == 0 && k < len_total){ //total length Odd && Even moves && moves < total lenght
return "No";
}
if (len_total % 2 == 0 && k % 2 != 0 && k < len_total){ //total length Even && Odd moves && moves < total lenght
return "No";
}
let min = len_s < len_t ? len_s : len_t;
for(let i = 0 ; i < min; i++){
if(s[i] != t[i]){
break;
}else{
idx = i + 1;
}
}
console.log(idx);
let max = len_s > len_t ? len_s : len_t;
if ((max - idx + min - idx) <= k){
return "Yes";
} else {
return "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