-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode-1417ReformatTheString.js
More file actions
68 lines (65 loc) · 1.67 KB
/
Copy pathleetcode-1417ReformatTheString.js
File metadata and controls
68 lines (65 loc) · 1.67 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
const regex = /^\d+$/;
/**
* @param {string} s
* @return {string}
*/
var reformat = function (s) {
s = s.split("");
let numerics = [],
alphabets = [];
s.forEach((char, index) => {
if (char.match(regex)) {
numerics.push(char);
} else {
alphabets.push(char);
}
});
// let numerics = s.filter((char) => char.match(regex));
// let alphabets = s.filter((char) => !char.match(regex));
let result = Math.abs(numerics.length - alphabets.length);
if (!(result === 1 || result === 0 || numerics.length === -1)) {
return "";
}
if (numerics.length >= alphabets.length) {
result = "";
for (let i = 0; i < alphabets.length; i++) {
result += numerics[i] + alphabets[i];
}
if (numerics.length > alphabets.length) {
result += numerics[numerics.length - 1];
}
} else {
result = "";
for (let i = 0; i < numerics.length; i++) {
result += alphabets[i] + numerics[i];
}
result += alphabets[alphabets.length - 1];
}
return result;
};
// Test case
if (reformat("a0b1c2") === "0a1b2c") {
console.log("Test case 1 is correct");
} else {
console.log("Test case 1 is incorrect");
}
if (reformat("leetcode") === "") {
console.log("Test case 2 is correct");
} else {
console.log("Test case 2 is incorrect");
}
if (reformat("1229857369") === "") {
console.log("Test case 3 is correct");
} else {
console.log("Test case 3 is incorrect");
}
if (reformat("covid2019") === "c2o0v1i9d") {
console.log("Test case 4 is correct");
} else {
console.log("Test case 4 is incorrect");
}
if (reformat("ab123") === "1a2b3") {
console.log("Test case 5 is correct");
} else {
console.log("Test case 5 is incorrect");
}