1

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.

3
  • what is the objective of the code? do you have an example and the wanted outcome? Commented Nov 19, 2019 at 10:36
  • Return true if a portion of str1 can be rearranged to form str2, otherwise return false. Commented Nov 19, 2019 at 10:39
  • I added a bit to the post hope this shows what the goal of the code is. Commented Nov 19, 2019 at 10:48

2 Answers 2

1

You could take a hash table, count the wanted characters and return if no count is not more necessary.

function scramble (str1, str2) { var counter = {}, keys = 0; for (let i = 0; i < str2.length; i++) { if (!counter[str2[i]]) { counter[str2[i]] = 0; keys++; } counter[str2[i]]++; } for (let i = 0; i < str1.length; i++) { if (!counter[str1[i]]) continue; if (!--counter[str1[i]] && !--keys) return true; } return false; } console.log(scramble('abc', 'cba')); console.log(scramble('abc', 'aba')); console.log(scramble('abcdea', 'aba')); console.log(scramble('aabbcamaomsccdd', 'commas'));

Sign up to request clarification or add additional context in comments.

2 Comments

This passes the long string cases, I thought there might be a way to make a regexp constructor and add the characters and the amount of each character in a string each time. Not sure if thats even a thing. I will need to look into hastables I guess not really familiar with them.
I will flag this as an answer if there is no regexp suggestion soon since that was what I thought the purpose of the challenge was. It works, is "faster" than my solution and gave me something else to learn/look into. Thanks.
1

if-less methodology!

i didnt stress it on tests but looks fine

function scramble(str1, str2) { str1 = [...str1]; return [...str2].filter((str => (str == str1.splice(str1.indexOf(str), 1)))).join('') == str2; } 

1 Comment

I wish this would work but the indexOf together with the other methods are killing it on the long string cases I think. Nice answer though gave me some ideas. This is the challenge I am working on if you want to have a look at the results. codewars.com/kata/scramblies/train/javascript

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.