2

I'm trying to solve this problem in JavaScript, but am having a hard time doing so.

Let's say that someone says a message like "yeah", I need to return "yyeeaahh"or if the frequency of each character was uneven in the original string:

"yyeaaahhh" then my function should return:

"yyyeeaaaahhhh"

My approach was as follows:

I started by turning the string into an Array of its characters.

After getting the Array, I decided to create a JavaScript object that has key value pairs in the sense where the keys are the letters and the values are how many times the letter appears in the string.

I then learned about the Object.entries() method which allows me to get an array of these key value pairs as arrays.

Where I am getting stuck is how do I map this array to a new array which contains the keys displayed (value + 1) times where value is the key's corresponding frequency in the original string.

In a more visual context I want go from this to this:

[['y', 2], ['e', 3], ['a', 3], ['h', 2]] => ['yyy', 'eeee', 'aaaa', 'hhh']

After this, I figure I can trivially join this array back into a string to get my resulting string.

Here's my code so far:

function newString(string) {
  const strArr = string.split('')
  var counts = {};
  for (var i = 0; i < strArr.length; i++) {
    if ((string.match(new RegExp(strArr[i], "g"))).length >= 1) {
      counts[string[i]] = (string.match(new RegExp(string[i], "g"))).length;
    }
  }
  var countArr = Object.entries(counts)
  var newStrArr = []
  for(const [key,value] in countArr) {
      newStrArr.push(key.repeat(value + 1))

  }
  return newStrArr 
}

[['y', 2], ['e', 3], ['a', 3], ['h', 2]] => ['yyy', 'eeee', 'aaaa', 'hhh']
1
  • 1
    What happens if runs are separated? e.g.: aba should it be aabbaa or aaabb or something else? Commented Jan 30, 2019 at 14:49

3 Answers 3

3

You could take the power of regular expression and look for a character which is not repeated and replace it with two of them.

var string = 'yeeaaahhhh';

console.log(string.replace(/(.)(?!\1)/g, '$&$&'));

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

1 Comment

If these were two separate answers I could vote correctly for the one I like, which is the string.replace(/(.)(?!\1)/g, '$&$&') technique.
1

Just create a string variable and concatenate. Loop through your created array and get the current letter and frequency of the current letter. Then create another loop to add the letter to the string frequency + 1 times.

var str = '';
for (var i = 0; i < newStrArr.length; i++) {
  var letter = newStrArr[i][0];
  var freq = newStrArr[i][1];
  for (var j = 0; j < freq + 1; j++) {
    str += letter;
  }
}

2 Comments

Just need to do (freq + 1) in the last for loop to add that extra character at the end...
Good catch, fixed
0

When you need to transform an array from some shape to another shape and you're not sure how, then you can use Array.reduce. This is a very powerful function that lets you use custom logic to recursively iterate over an array.

const arr = [['y', 2], ['e', 3], ['a', 3], ['h', 2]]

const word = arr.reduce((newWord, currentLetter) => {

  //deconstructing my variables
  const [letter, count] = currentLetter 

  //use a loop and a template literal to alter our string
  //using less than equals we can include 0 in the count
  //effectively adding an extra letter
  for(let i = 0; i <= count; i++) {
    newWord = `${newWord}${letter}`
  }

  //when reducing, don't forget to return your value
  return newWord
}, '')

console.log(word) // --> yyyeeeeaaaahhh

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.