Skip to content

Commit 5f8bba4

Browse files
Merge pull request #208 from TheGeniusOfEternity/feature/interview/digit-permutation
Feat | Interview | #16 Digit Permutation
2 parents 6155a49 + fd4291c commit 5f8bba4

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,6 @@ Interview Questions
133133
- [Task #11](src/interview/build-tree/no-replies-sort) - _Build Tree: No Replies Sort_
134134
- [Task #12](src/interview/build-tree/with-replies-sort) - _Build Tree: With Replies Sort_
135135
- [Task #13](src/interview/build-comments-tree) - _Build Comments Tree_
136-
- [Task #13](src/interview/find-by-type) - _Find By Type_
137-
- [Task #14](src/interview/group-numbers) - _Group Numbers_
136+
- [Task #14](src/interview/find-by-type) - _Find By Type_
137+
- [Task #15](src/interview/group-numbers) - _Group Numbers_
138+
- [Task #16](src/interview/digit-permutation) - _Digit Permutation_
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { digitPermutation } from './solution';
3+
4+
describe('Digit Permutation | Interview | Testcases', () => {
5+
it('#1 Should return true for numbers with same nonzero digits in same count (ignoring zeros)', () => {
6+
expect(digitPermutation(10023, 321)).toBe(true);
7+
expect(digitPermutation(1000, 1)).toBe(true);
8+
expect(digitPermutation(123, 123)).toBe(true);
9+
expect(digitPermutation(122, 212)).toBe(true);
10+
expect(digitPermutation(122, 221)).toBe(true);
11+
expect(digitPermutation(1001, 11)).toBe(true);
12+
expect(digitPermutation(1010, 11)).toBe(true);
13+
});
14+
15+
it('#2 Should return false when nonzero digit counts differ', () => {
16+
expect(digitPermutation(112, 12)).toBe(false);
17+
expect(digitPermutation(123, 124)).toBe(false);
18+
expect(digitPermutation(122, 22)).toBe(false);
19+
expect(digitPermutation(1234, 4321)).toBe(true);
20+
expect(digitPermutation(1234, 43210)).toBe(true);
21+
expect(digitPermutation(1234, 4322)).toBe(false);
22+
});
23+
24+
it('#3 Should handle numbers with only zeros and a single nonzero digit', () => {
25+
expect(digitPermutation(1000, 1)).toBe(true);
26+
expect(digitPermutation(10000, 1)).toBe(true);
27+
expect(digitPermutation(1, 1000)).toBe(true);
28+
});
29+
30+
it('#4 Should treat numbers with no nonzero digits as equal', () => {
31+
expect(digitPermutation(0, 0)).toBe(true);
32+
expect(digitPermutation(0, 10)).toBe(false);
33+
});
34+
35+
it('#5 Should work for large numbers', () => {
36+
expect(digitPermutation(123456789, 987654321)).toBe(true);
37+
expect(digitPermutation(123456789, 9876543210)).toBe(true);
38+
expect(digitPermutation(123456789, 9876543211)).toBe(false);
39+
});
40+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
*
3+
* @param a - positive integer
4+
* @param b - positive integer
5+
* @returns `true` if `a` contains of the same amount of digits (except `0`) as `b`
6+
*
7+
*`false` otherwise
8+
*
9+
* @example
10+
* console.log(digitPermutation(10023, 321)); // true
11+
* console.log(digitPermutation(112, 12)); // false
12+
* console.log(digitPermutation(1000, 1)); // true
13+
*/
14+
export const digitPermutation = (a: number, b: number): boolean => {
15+
const count = new Array<number>(10).fill(0);
16+
17+
for (const d of a.toString()) {
18+
count[Number(d)]++;
19+
}
20+
21+
for (const d of b.toString()) {
22+
count[Number(d)]--;
23+
}
24+
25+
for (let i = 1; i < 10; i++) {
26+
if (count[i] !== 0) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
};

0 commit comments

Comments
 (0)