-
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.
- Loading branch information
Showing
9 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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') | ||
}) |
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,70 @@ | ||
# Increasing Decreasing String | ||
|
||
LeetCode #: [1370](https://leetcode.com/problems/increasing-decreasing-string/) | ||
|
||
Difficulty: Easy | ||
|
||
Topic: String, sort. | ||
|
||
## Problem | ||
|
||
Given a string `s`. You should re-order the string using the following algorithm: | ||
|
||
1. Pick the smallest character from `s` and append it to the result. | ||
2. Pick the smallest character from `s` which is greater than the last appended character to the result and append it. | ||
3. Repeat step 2 until you cannot pick more characters. | ||
4. Pick the largest character from `s` and append it to the result. | ||
5. Pick the largest character from `s` which is smaller than the last appended character to the result and append it. | ||
6. Repeat step 5 until you cannot pick more characters. | ||
7. Repeat the steps from 1 to 6 until you pick all characters from `s`. | ||
|
||
In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result. | ||
|
||
Return the result string after sorting `s` with this algorithm. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: s = "aaaabbbbcccc" | ||
Output: "abccbaabccba" | ||
Explanation: | ||
After steps 1, 2 and 3 of the first iteration, result = "abc" | ||
After steps 4, 5 and 6 of the first iteration, result = "abccba" | ||
First iteration is done. Now s = "aabbcc" and we go back to step 1 | ||
After steps 1, 2 and 3 of the second iteration, result = "abccbaabc" | ||
After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba" | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: s = "rat" | ||
Output: "art" | ||
Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm. | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: s = "leetcode" | ||
Output: "cdelotee" | ||
``` | ||
|
||
Example 4: | ||
|
||
```text | ||
Input: s = "ggggggg" | ||
Output: "ggggggg" | ||
``` | ||
|
||
Example 5: | ||
|
||
```text | ||
Input: s = "spo" | ||
Output: "ops" | ||
``` | ||
|
||
Constraints: | ||
|
||
- `1 <= s.length <= 500` | ||
- `s` contains only lower-case English letters. |
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,70 @@ | ||
const getMap = (s) => { | ||
const chars = [ | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', | ||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', | ||
'u', 'v', 'w', 'x', 'y', 'z' | ||
] | ||
const counts = [ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0 | ||
] | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
const charCode = s[i].charCodeAt(0) | ||
counts[charCode - 97]++ | ||
} | ||
|
||
for (let i = 0; i < counts.length; i++) { | ||
if (counts[i] === 0) { | ||
counts.splice(i, 1) | ||
chars.splice(i, 1) | ||
|
||
i-- | ||
} | ||
} | ||
|
||
return { | ||
countMap: counts, | ||
charMap: chars | ||
} | ||
} | ||
|
||
const sortString = (s) => { | ||
const { countMap, charMap } = getMap(s) | ||
|
||
let result = '' | ||
let dir = true | ||
let i = 0 | ||
do { | ||
result += charMap[i] | ||
countMap[i]-- | ||
|
||
if (countMap[i] === 0) { | ||
countMap.splice(i, 1) | ||
charMap.splice(i, 1) | ||
|
||
if (dir === true) { | ||
i-- | ||
} | ||
} | ||
|
||
if (dir === true) { | ||
i++ | ||
} else { | ||
i-- | ||
} | ||
|
||
if (i === charMap.length) { | ||
dir = false | ||
i-- | ||
} else if (i === -1) { | ||
dir = true | ||
i++ | ||
} | ||
} while (countMap.length > 0) | ||
|
||
return result | ||
} | ||
|
||
module.exports = sortString |
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,41 @@ | ||
const sortString = require('./sortString') | ||
|
||
test('Example 1', () => { | ||
const s = 'aaaabbbbcccc' | ||
|
||
const result = sortString(s) | ||
|
||
expect(result).toBe('abccbaabccba') | ||
}) | ||
|
||
test('Example 2', () => { | ||
const s = 'rat' | ||
|
||
const result = sortString(s) | ||
|
||
expect(result).toBe('art') | ||
}) | ||
|
||
test('Example 3', () => { | ||
const s = 'leetcode' | ||
|
||
const result = sortString(s) | ||
|
||
expect(result).toBe('cdelotee') | ||
}) | ||
|
||
test('Example 4', () => { | ||
const s = 'ggggggg' | ||
|
||
const result = sortString(s) | ||
|
||
expect(result).toBe('ggggggg') | ||
}) | ||
|
||
test('Example 5', () => { | ||
const s = 'spo' | ||
|
||
const result = sortString(s) | ||
|
||
expect(result).toBe('ops') | ||
}) |