Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@ case characters are distinct from lower case ones i.e. "Alex" is not a permutati
of "alex". We define a permutation as a rearrangement of characters i.e. two strings
that are permutations of each other must contain exactly the same characters with the
same frequency. If two strings have unequal length they cannot be permutations.
We count character appearances using a 128 wide bit vector. We
traverse each string one character at a time. Each time we observe a character, we flip
the bit corresponding at the index corresponding to its ASCII value. If we traverse two
strings which both contain exactly the same number of characters with the same character
frequency, the bit vector will be all 0s at the end of the two traversals. This is because
for two strings that are permutations, each bit will be flipped an even number of
times in total.
We count character appearances using an int array of size 128. We
traverse each string one character at a time. Each time we observe a character,
we increase the count by 1, while traversing the second we decrease the count by 1 for the same character.
If both the strings do match then the array will be in its initial state with all zeroes, if they don't match the count will become less than 0 for the disimilar character.

Time complexity: O(N) where N is the length of the linearly traversed strings.
Space complexity: O(1) because bit vector does not scale with string length.
Space complexity: O(1) because array does not scale with string length.
*/

#include "problem_01_02_arePermutations.h"
#include <bitset>


bool chapter_01::isPermutation(const std::string& s1, const std::string& s2){
Expand Down