InterviewBit: Self Permutation Solution

Count number of letters

Approach

Permutation exists if the letter counts in both string are equal

Implementation

1function permuteStrings(A, B) {
2 const [countA, countB] = [new Uint8Array(26), new Uint8Array(26)]
3
4 A.split("").forEach(char => countA[char.charCodeAt(0) - "a".charCodeAt(0)]++)
5 B.split("").forEach(char => countB[char.charCodeAt(0) - "a".charCodeAt(0)]++)
6
7 for (let i = 0; i < 26; i++) {
8 if (countA[i] !== countB[i]) return 0
9 }
10
11 return 1
12}

References

Original problem

Similar problems

Amazing Subarrays

Palindrome String

Minimum Characters required to make a String Palindromic

Comments

Loading comments...

Tags

interviewbit

string

Next Post

LeetCode: Number of Ways to Split a String

Playing with indexes

Previous Post

LeetCode: Minimum ASCII Delete Sum for Two Strings

Modified version of Longest Common Subsequence problem

HoningJS

Search Posts