Skip to content

Commit 299f90e

Browse files
committed
lexicographicallySmallestString.
1 parent 6e158ca commit 299f90e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 05/02/2020
4+
*/
5+
6+
const lexicographicallySmallestString = (str) => {
7+
/*
8+
* Steps:
9+
* 1. Define, 2. Represent, 3. Approach/Strategy, 4. Algorithm, 5. Analyse, 6. Experiment
10+
*/
11+
// "abczd" => "abcd"
12+
const result = str.split("")
13+
.map(x => x.charCodeAt(0))
14+
.sort((a, b) => a - b)
15+
.slice(0, -1)
16+
.map(x => String.fromCharCode(x))
17+
.join("");
18+
return result;
19+
}
20+
21+
module.exports = lexicographicallySmallestString;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 05/02/2020
4+
*/
5+
const lexicalSmallestString = require('./index');
6+
test('lexicalSmallestString is a function', () => {
7+
expect(typeof lexicalSmallestString).toEqual('function');
8+
});
9+
test('lexicalSmallestString "abczd" to give abcd', () => {
10+
expect(lexicalSmallestString("abczd")).toEqual('abcd');
11+
});
12+
test('lexicalSmallestString "klmoyp" to give klmop', () => {
13+
expect(lexicalSmallestString("klmoyp")).toEqual("klmop");
14+
});
15+
test('lexicalSmallestString "fghixj" to give "fghij"', () => {
16+
expect(lexicalSmallestString("fghixj")).toEqual("fghij");
17+
});

0 commit comments

Comments
 (0)