Skip to content

Commit 0018997

Browse files
committed
Add solution #1417
1 parent 548d4f9 commit 0018997

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,292 LeetCode solutions in JavaScript
1+
# 1,293 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1083,6 +1083,7 @@
10831083
1414|[Find the Minimum Number of Fibonacci Numbers Whose Sum Is K](./solutions/1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k.js)|Medium|
10841084
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
10851085
1416|[Restore The Array](./solutions/1416-restore-the-array.js)|Hard|
1086+
1417|[Reformat The String](./solutions/1417-reformat-the-string.js)|Easy|
10861087
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
10871088
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
10881089
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|

solutions/1417-reformat-the-string.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1417. Reformat The String
3+
* https://leetcode.com/problems/reformat-the-string/
4+
* Difficulty: Easy
5+
*
6+
* You are given an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase
7+
* English letters and digits).
8+
*
9+
* You have to find a permutation of the string where no letter is followed by another letter and
10+
* no digit is followed by another digit. That is, no two adjacent characters have the same type.
11+
*
12+
* Return the reformatted string or return an empty string if it is impossible to reformat the
13+
* string.
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @return {string}
19+
*/
20+
var reformat = function(s) {
21+
const letters = [];
22+
const digits = [];
23+
for (const char of s) {
24+
if (isNaN(char)) {
25+
letters.push(char);
26+
} else {
27+
digits.push(char);
28+
}
29+
}
30+
if (Math.abs(letters.length - digits.length) > 1) {
31+
return '';
32+
}
33+
const result = [];
34+
const longer = letters.length >= digits.length ? letters : digits;
35+
const shorter = longer === letters ? digits : letters;
36+
for (let i = 0; i < longer.length; i++) {
37+
result.push(longer[i]);
38+
if (i < shorter.length) {
39+
result.push(shorter[i]);
40+
}
41+
}
42+
return result.join('');
43+
};

0 commit comments

Comments
 (0)