Skip to content

Commit

Permalink
added solution for #1374 Generate a String With Characters That Have …
Browse files Browse the repository at this point in the history
…Odd Counts.
  • Loading branch information
ecgan committed Mar 8, 2020
1 parent ee87391 commit 245acb7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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`
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
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')
})

0 comments on commit 245acb7

Please sign in to comment.