From dc50de48ab84fc44b5cdf16ca766d39adaad7735 Mon Sep 17 00:00:00 2001 From: jerinthomas1404 Date: Fri, 3 Dec 2021 00:06:16 +0530 Subject: [PATCH] [Action Item] Removed : Occurences of bit set, as it has not been used; Added : Array has been used for the frequency count, so I have added the comment with array part, modified the comment based on the approach; --- .../problem_01_02_arePermutations.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cpp_solutions/chapter_01_arrays_and_strings/problem_01_02_arePermutations.cpp b/cpp_solutions/chapter_01_arrays_and_strings/problem_01_02_arePermutations.cpp index 9f697e3..ada4c82 100644 --- a/cpp_solutions/chapter_01_arrays_and_strings/problem_01_02_arePermutations.cpp +++ b/cpp_solutions/chapter_01_arrays_and_strings/problem_01_02_arePermutations.cpp @@ -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 bool chapter_01::isPermutation(const std::string& s1, const std::string& s2){