Skip to content

Commit 920ef1d

Browse files
committed
Add solution #1400
1 parent c3dcca6 commit 920ef1d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
1374|[Generate a String With Characters That Have Odd Counts](./1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
312312
1380|[Lucky Numbers in a Matrix](./1380-lucky-numbers-in-a-matrix.js)|Easy|
313313
1389|[Create Target Array in the Given Order](./1389-create-target-array-in-the-given-order.js)|Easy|
314+
1400|[Construct K Palindrome Strings](./1400-construct-k-palindrome-strings.js)|Medium|
314315
1402|[Reducing Dishes](./1402-reducing-dishes.js)|Hard|
315316
1408|[String Matching in an Array](./1408-string-matching-in-an-array.js)|Easy|
316317
1410|[HTML Entity Parser](./1410-html-entity-parser.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 1400. Construct K Palindrome Strings
3+
* https://leetcode.com/problems/construct-k-palindrome-strings/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s and an integer k, return true if you can use all the characters in s to
7+
* construct k palindrome strings or false otherwise.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @param {number} k
13+
* @return {boolean}
14+
*/
15+
var canConstruct = function(s, k) {
16+
const occurrences = new Array(26).fill(0);
17+
s.split('').forEach(c => occurrences[c.charCodeAt(0) - 'a'.charCodeAt(0)]++);
18+
19+
const oddCount = occurrences.filter(n => n % 2 !== 0).length;
20+
return k <= s.length && oddCount <= k;
21+
};

0 commit comments

Comments
 (0)