Skip to content

Commit

Permalink
Merge branch 'release/1.19.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Mar 8, 2020
2 parents d847a9f + 13ee29e commit 1d0c457
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Folder structure in this repository: https://<span></span>github.com/ecgan/leetc
| 1360 | [Number of Days Between Two Dates](/problems/number-of-days-between-two-dates) | Easy | - |
| 1361 | [Validate Binary Tree Nodes](/problems/validate-binary-tree-nodes) | Medium | Graph |
| 1362 | [Closest Divisors](/problems/closest-divisors) | Medium | Math |
| 1370 | [Increasing Decreasing String](/problems/increasing-decreasing-string) | Easy | String, sort |
| 1374 | [Generate a String With Characters That Have Odd Counts](/problems/generate-a-string-with-characters-that-have-odd-counts) | Easy | String |

## Questions / Issues

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.18.0",
"version": "1.19.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
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')
})
70 changes: 70 additions & 0 deletions problems/increasing-decreasing-string/README.md
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.
70 changes: 70 additions & 0 deletions problems/increasing-decreasing-string/sortString.js
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
41 changes: 41 additions & 0 deletions problems/increasing-decreasing-string/sortString.test.js
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')
})

0 comments on commit 1d0c457

Please sign in to comment.