Skip to content

Commit 3baa43e

Browse files
committed
add: RunLengthEncoding function implementation
1 parent 4c0afc3 commit 3baa43e

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
- **Strings**
3030
- [CaesarCipherEncryptor](Strings/CaesarCipherEncryptor.js)
3131
- [PalindromeCheck](Strings/PalindromeCheck.js)
32+
- [RunLengthEncoding](Strings/RunLengthEncoding.js)

Strings/RunLengthEncoding.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Encodes a given string using run-length encoding.
3+
*
4+
* Run-length encoding is a lossless data compression technique where runs of
5+
* consecutive identical characters are replaced with a count and the character itself.
6+
*
7+
* @param {string} string - The input string to be encoded.
8+
* @returns {string} - The run-length encoded string.
9+
*
10+
* @example
11+
* const input = "AAAAAAAAAAAAABBCCCCDD";
12+
* const encodedString = runLengthEncoding(input); // Output: "9A4A2B4C2D"
13+
*
14+
* @complexity
15+
* Time Complexity: O(n), where n is the length of the input string. We iterate through the string once.
16+
* Space Complexity: O(m), where m is the length of the encoded string. In the worst case, each character is encoded separately.
17+
*/
18+
function runLengthEncoding(string) {
19+
let encoded = [];
20+
let run = 1;
21+
22+
for (let i = 0; i < string.length; i++) {
23+
let letter = string[i];
24+
if (letter === string[i + 1] && run < 9) {
25+
run++;
26+
} else {
27+
encoded.push(`${run}${letter}`);
28+
run = 1;
29+
}
30+
}
31+
32+
return encoded.join("");
33+
}
34+
35+
export { runLengthEncoding };
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { runLengthEncoding } from "../RunLengthEncoding";
2+
3+
describe("runLengthEncoding", () => {
4+
it("should encode a string with consecutive characters correctly", () => {
5+
const input = "AAABBBCCCDDEE";
6+
const expectedOutput = "3A3B3C2D2E";
7+
const encodedString = runLengthEncoding(input);
8+
expect(encodedString).toBe(expectedOutput);
9+
});
10+
11+
it("should handle single characters without encoding", () => {
12+
const input = "aA";
13+
const expectedOutput = "1a1A";
14+
const encodedString = runLengthEncoding(input);
15+
expect(encodedString).toBe(expectedOutput);
16+
});
17+
18+
it("should handle special characters", () => {
19+
const input = "!@##$$$";
20+
const expectedOutput = "1!1@2#3$";
21+
const encodedString = runLengthEncoding(input);
22+
expect(encodedString).toBe(expectedOutput);
23+
});
24+
25+
it("should handle a mix of characters", () => {
26+
const input = "AABBB!@";
27+
const expectedOutput = "2A3B1!1@";
28+
const encodedString = runLengthEncoding(input);
29+
expect(encodedString).toBe(expectedOutput);
30+
});
31+
32+
it("should handle a string of consecutive numbers", () => {
33+
const input = "122333";
34+
const expectedOutput = "112233";
35+
const encodedString = runLengthEncoding(input);
36+
expect(encodedString).toBe(expectedOutput);
37+
});
38+
39+
it("should handle a string of consecutive special characters", () => {
40+
const input = "************^^^^^^^$$$$$$%%%%%%%!!!!!!AAAAAAAAAAAAAAAAAAAA";
41+
const expectedOutput = "9*3*7^6$7%6!9A9A2A";
42+
const encodedString = runLengthEncoding(input);
43+
expect(encodedString).toBe(expectedOutput);
44+
});
45+
46+
it("should handle a mix of lowercase, uppercase, and numbers", () => {
47+
const input = "aAaAaaaaaAaaaAAAABbbbBBBB";
48+
const expectedOutput = "1a1A1a1A5a1A3a4A1B3b4B";
49+
const encodedString = runLengthEncoding(input);
50+
expect(encodedString).toBe(expectedOutput);
51+
});
52+
53+
it("should handle an empty string", () => {
54+
const input = "";
55+
const expectedOutput = "";
56+
const encodedString = runLengthEncoding(input);
57+
expect(encodedString).toBe(expectedOutput);
58+
});
59+
60+
it("should handle a string with spaces and numbers", () => {
61+
const input = "1 222 333 444 555";
62+
const expectedOutput = "112 321 334 342 35";
63+
const encodedString = runLengthEncoding(input);
64+
expect(encodedString).toBe(expectedOutput);
65+
});
66+
});

0 commit comments

Comments
 (0)