I am doing doing a few code challenges in the hope to learn some new stuff. Currently I have written a piece of code that find charachter of a given word in a string of random letters.
I thought regexp might be the best performance-wise (it's one of the objectives). This code passes the checks but takes too long with absurd long strings. Is there any way I can improve this? It's really ugly as is honestly. I've tried several approaches but I am probably just really a newbie at reg exp etc.
before all the if statements I only used regexp but if str2 which is the word I am looking for had double characters it would come back 'true' because it would count already counted characters. That is why I am using replace to exclude them. That's all I could get.
the goal is to return true if a portion of str1 can be rearranged to form str2, otherwise return false. Only lower case letters will be used (a-z). No punctuation or digits will be included. for example scramble('aabbcamaomsccdd','commas') should return true
function scramble (str1, str2) { var o = 0; // tracks amount of matched letters. for(i = 0; i < str2.length; i++) { var regex1 = new RegExp (str2[i]) ; // select letter from word that needs to be found if( regex1.test(str1) == true)// if selected character is found us replace to remove it from the random characters string for next iteration. { str1 = str1.replace(regex1 ,""); o++; // increment o if character is removed from random string. } } //check if amount of removed characters equals total characters of word that we want. if ( o == str2.length) { return true } if (o !== str2.length) { return false } } Update: I flagged the hash table as answer because afaik this was not doable with regexp it seems also I was able to achieve a proper result with .split and loops myself plus the hash table also achieved this.