Skip to content

Commit a62c27d

Browse files
committed
Add: Exercise 443. String Compression
1 parent e8b37ba commit a62c27d

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

443-string-compression/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 443. String Compression
2+
Given an array of characters `chars`, compress it using the following algorithm:
3+
4+
Begin with an empty string `s`. For each group of consecutive repeating characters in `chars`:
5+
6+
- If the group's length is 1, append the character to `s`.
7+
- Otherwise, append the character followed by the group's length.
8+
9+
The compressed string `s` should not be returned separately, but instead, be stored in the input character array `chars`. Note that group lengths that are 10 or longer will be split into multiple characters in `chars`.
10+
11+
After you are done modifying the input array, return the new length of the array.
12+
13+
You must write an algorithm that uses only constant extra space.
14+
15+
## Examples
16+
17+
### Example 1:
18+
Input: `chars` = ["a","a","b","b","c","c","c"]
19+
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
20+
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
21+
22+
### Example 2:
23+
Input: `chars` = ["a"]
24+
Output: Return 1, and the first character of the input array should be: ["a"]
25+
Explanation: The only group is "a", which remains uncompressed since it's a single character.
26+
27+
### Example 3:
28+
Input: `chars` = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
29+
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
30+
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
31+
32+
## Constraints
33+
- 1 <= `chars.length` <= 2000
34+
- `chars[i]` is a lowercase English letter, uppercase English letter, digit, or symbol.

443-string-compression/index.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {character[]} chars
3+
* @return {number}
4+
*/
5+
/**
6+
* @param {character[]} chars
7+
* @return {number}
8+
*/
9+
var compress = function(chars) {
10+
// Intial conditions
11+
const compressed = [chars[0]]
12+
let prevChar = chars[0]
13+
let charCount = 1
14+
15+
// Traverse the array, if the current char is the same previous one, add a counter
16+
for (let i = 1; i <= chars.length; i++) {
17+
if (chars[i] !== prevChar) {
18+
// If `charCount` is multicharacter, split it in (i.e. '12' => '1', '2')
19+
if (charCount.toString().length > 1) {
20+
compressed.pop()
21+
compressed.push(...Array.from(charCount.toString()))
22+
}
23+
24+
if (i === chars.length) {
25+
break
26+
}
27+
28+
prevChar = chars[i]
29+
charCount = 1
30+
compressed.push(chars[i])
31+
continue
32+
}
33+
34+
charCount += 1
35+
if (charCount === 2) {
36+
compressed.push(charCount.toString())
37+
continue
38+
}
39+
40+
compressed[compressed.length - 1] = charCount.toString()
41+
}
42+
43+
// Adds the compressed array at the begginning of the chars array
44+
chars.unshift(...compressed)
45+
return compressed.length
46+
};
47+
48+
module.exports = compress

443-string-compression/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "443-string-compression",
3+
"version": "1.0.0",
4+
"description": "Given an array of characters `chars`, compress it using the following algorithm:",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

443-string-compression/test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require('assert')
2+
const compress = require('./index')
3+
4+
const tests = [
5+
{
6+
chars: ['a','a','b','b','c','c','c'],
7+
expectedLength: 6,
8+
expectedCompressed: ['a','2','b','2','c','3'],
9+
},
10+
{
11+
chars: ['a'],
12+
expectedLength: 1,
13+
expectedCompressed: ['a'],
14+
},
15+
{
16+
chars: ['a','b','b','b','b','b','b','b','b','b','b','b','b'],
17+
expectedLength: 4,
18+
expectedCompressed: ['a','b','1','2'],
19+
},
20+
]
21+
22+
for (const t of tests) {
23+
assert.strict.deepEqual(compress(t.chars), t.expectedLength)
24+
assert.strict.deepEqual(t.chars.slice(0, t.expectedLength), t.expectedCompressed)
25+
}

0 commit comments

Comments
 (0)