From 245acb711da22fb21be515238da6f9b016efce48 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 8 Mar 2020 14:51:13 +0800 Subject: [PATCH] added solution for #1374 Generate a String With Characters That Have Odd Counts. --- .../README.md | 40 +++++++++++++++++++ .../generateTheString.js | 17 ++++++++ .../generateTheString.test.js | 17 ++++++++ 3 files changed, 74 insertions(+) create mode 100644 problems/generate-a-string-with-characters-that-have-odd-counts/README.md create mode 100644 problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.js create mode 100644 problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.test.js diff --git a/problems/generate-a-string-with-characters-that-have-odd-counts/README.md b/problems/generate-a-string-with-characters-that-have-odd-counts/README.md new file mode 100644 index 0000000..df71a9f --- /dev/null +++ b/problems/generate-a-string-with-characters-that-have-odd-counts/README.md @@ -0,0 +1,40 @@ +# Generate a String With Characters That Have Odd Counts + +LeetCode #: [1374](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) + +Difficulty: Easy + +Topic: String. + +## Problem + +Given an integer `n`, return a string with `n` characters such that each character in such string occurs an odd number of times. + +The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them. + +Example 1: + +```text +Input: n = 4 +Output: "pppz" +Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love". +``` + +Example 2: + +```text +Input: n = 2 +Output: "xy" +Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur". +``` + +Example 3: + +```text +Input: n = 7 +Output: "holasss" +``` + +Constraints: + +* `1 <= n <= 500` diff --git a/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.js b/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.js new file mode 100644 index 0000000..1dd81f0 --- /dev/null +++ b/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.js @@ -0,0 +1,17 @@ +const generateTheString = (n) => { + let result = '' + let count = n + + if (n % 2 === 0) { + result += 'a' + count-- + } + + for (let i = 0; i < count; i++) { + result += 'b' + } + + return result +} + +module.exports = generateTheString diff --git a/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.test.js b/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.test.js new file mode 100644 index 0000000..944c984 --- /dev/null +++ b/problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.test.js @@ -0,0 +1,17 @@ +const toHexspeak = require('./generateTheString') + +test('Example even', () => { + const n = 4 + + const result = toHexspeak(n) + + expect(result).toBe('abbb') +}) + +test('Example odd', () => { + const n = 5 + + const result = toHexspeak(n) + + expect(result).toBe('bbbbb') +})