-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added solution for #1374 Generate a String With Characters That Have …
…Odd Counts.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
problems/generate-a-string-with-characters-that-have-odd-counts/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
17 changes: 17 additions & 0 deletions
17
problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
17 changes: 17 additions & 0 deletions
17
problems/generate-a-string-with-characters-that-have-odd-counts/generateTheString.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) |