Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aae5ade

Browse files
Merge pull request knaxus#23 from marcelinol/add-test-for-string-permutation
Add test for string permutation
2 parents c57b51d + f1a551c commit aae5ade

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
3232
- [Find 2 numbers that add upto N](src/_Problems_/find-2-nums-adding-to-n)
3333
- [Find 2nd Maxinum from an Array](src/_Problems_/find-2nd-max)
3434
- [FizzBuzz](src/_Problems_/fizzbuzz)
35-
- [String Permutaions](src/_Problems_/get_string_permutations)
35+
- [String Permutaions](src/_Problems_/get-string-permutations)
3636
- [Get Subsequence](src/_Problems_/get_subsequence)
3737
- [Get Maze Path](src/_Problems_/get_subsequence)
3838
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { getPermutations } = require('.');
2+
3+
describe('Get permutations of a string', () => {
4+
it('returns permutations of a short string', () => {
5+
const shortString = 'ab';
6+
const expectedPermutations = ['ab', 'ba'];
7+
8+
expect(getPermutations(shortString)).toEqual(expectedPermutations);
9+
});
10+
11+
it('returns permutations of a long string', () => {
12+
const shortString = 'XUNDA';
13+
const expectedPermutations = ['XUNDA', 'UXNDA', 'NXUDA', 'XNUDA', 'UNXDA', 'NUXDA', 'DUXNA', 'UDXNA', 'XDUNA', 'DXUNA', 'UXDNA', 'XUDNA', 'XNDUA', 'NXDUA', 'DXNUA', 'XDNUA', 'NDXUA', 'DNXUA', 'DNUXA', 'NDUXA', 'UDNXA', 'DUNXA', 'NUDXA', 'UNDXA', 'ANDXU', 'NADXU', 'DANXU', 'ADNXU', 'NDAXU', 'DNAXU', 'XNADU', 'NXADU', 'AXNDU', 'XANDU', 'NAXDU', 'ANXDU', 'ADXNU', 'DAXNU', 'XADNU', 'AXDNU', 'DXANU', 'XDANU', 'XDNAU', 'DXNAU', 'NXDAU', 'XNDAU', 'DNXAU', 'NDXAU', 'UDXAN', 'DUXAN', 'XUDAN', 'UXDAN', 'DXUAN', 'XDUAN', 'ADUXN', 'DAUXN', 'UADXN', 'AUDXN', 'DUAXN', 'UDAXN', 'UXADN', 'XUADN', 'AUXDN', 'UAXDN', 'XAUDN', 'AXUDN', 'AXDUN', 'XADUN', 'DAXUN', 'ADXUN', 'XDAUN', 'DXAUN', 'NXAUD', 'XNAUD', 'ANXUD', 'NAXUD', 'XANUD', 'AXNUD', 'UXNAD', 'XUNAD', 'NUXAD', 'UNXAD', 'XNUAD', 'NXUAD', 'NAUXD', 'ANUXD', 'UNAXD', 'NUAXD', 'AUNXD', 'UANXD', 'UAXND', 'AUXND', 'XUAND', 'UXAND', 'AXUND', 'XAUND', 'DAUNX', 'ADUNX', 'UDANX', 'DUANX', 'AUDNX', 'UADNX', 'NADUX', 'ANDUX', 'DNAUX', 'NDAUX', 'ADNUX', 'DANUX', 'DUNAX', 'UDNAX', 'NDUAX', 'DNUAX', 'UNDAX', 'NUDAX', 'NUADX', 'UNADX', 'ANUDX', 'NAUDX', 'UANDX', 'AUNDX'];
14+
15+
expect(getPermutations(shortString).sort()).toEqual(expectedPermutations.sort());
16+
});
17+
18+
it('returns the same string if the string is one character long', () => {
19+
const shortString = 'a';
20+
const expectedPermutations = ['a'];
21+
22+
expect(getPermutations(shortString)).toEqual(expectedPermutations);
23+
});
24+
25+
it('returns an empty array for an empty string', () => {
26+
const shortString = '';
27+
const expectedPermutations = [];
28+
29+
expect(getPermutations(shortString)).toEqual(expectedPermutations);
30+
});
31+
32+
it('is case sensitive', () => {
33+
const shortString = 'aB';
34+
35+
expect(getPermutations(shortString)).not.toEqual(['ab', 'ba']);
36+
expect(getPermutations(shortString)).toEqual(['aB', 'Ba']);
37+
});
38+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// GET PERMUTATION OF A GIVEN STRING
2+
3+
const getPermutations = (str) => {
4+
let result = [];
5+
6+
if (str.length == 0) {
7+
return result;
8+
}
9+
10+
if (str.length == 1) {
11+
result.push(str);
12+
return result;
13+
}
14+
15+
let currentCharacter = str.charAt(0);
16+
let restOfString = str.substring(1);
17+
let returnResult = getPermutations(restOfString);
18+
19+
for (j = 0; j < returnResult.length; j++) {
20+
for (i = 0; i <= returnResult[j].length; i++) {
21+
let value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
22+
result.push(value);
23+
}
24+
}
25+
26+
return result;
27+
};
28+
29+
module.exports = { getPermutations };

‎src/_Problems_/get_string_permutations/index.js‎

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /