Created
October 31, 2018 04:38
-
-
Save hluu5/84280316df8efc85a2776844eb6f9a76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Specification: Input: 2 strings | |
//Output: boolean, if they are anagram of each other; | |
//Constraints: ignore capitalization; | |
//edge case: return false; | |
//Justification: check if one of the strings is a rearragement of characters the other; | |
//Explanation: two strings that have characters, it can be or can not be a rearrangment of characters of the other; | |
//Visualization: | |
string1 => S T R I N G 1 | |
string2 => S T R I N G 2 | |
Compare size. Sort them. then Check; | |
//Approximation/ Pseudocode: | |
1. Check length => if diference then return false; else go to 2nd step; | |
2. Split the 2 strings into characters, put them into 2 arrays ; | |
3. Sort the 2 arrays by words; | |
4. Then join the sorted arrays into strings again; | |
5. Compare if the two strings are the same; | |
6. Return true if it is, else false; | |
//Verification: | |
Ex: help , kelp | |
1. Check length; length of characters = 4 on both => go to step 2; | |
2. Split 2 strings and put them into arrays: | |
help => [h, e, l, p]; | |
kelp => [k, e, l, p]; | |
3. Sort the 2 arrays by words: | |
[h, e, l, p] => ["e", "h", "l", "p"] | |
[k, e, l, p] => ["e", "k", "l", "p"] | |
4. Join characters together: | |
["e", "h", "l", "p"] => ehlp | |
["e", "k", "l", "p"] => eklp | |
5. Compare two strings again; | |
ehlp =/= eklp; | |
6. Return false; | |
//Real Code: | |
function checkAnagram(str1, str2) { | |
if (str1.length !== str2.length) { | |
return false; | |
} | |
var arr1 = str1.split('').join(); | |
var arr2 = str2.split('').join(); | |
return arr1 = arr2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment