2

I was doing a codewar challange, and couldn't find a solution, but I really want to know how we can solve this problem.

So we getting two integers, let's say N and D and we should return a string containing exactly N letters 'n' and exactlly D letters d with no three consecutive letters being same.

For example if we get N=5 and D=3 we should return "nndnndnd" or "nbnnbbnn" or any other correct answer

another example like if we get N=1 D=4 the only accepted answer should be "ddndd"

What I did was making a helper function like this :

function generateArray (char,q){
let arr= []
for(let i=0; i<q; i++){
    arr.push(char)
}

return arr
}

and inside the main function :

function solution(N, D) {
let arrayOfchar = generateArray('n',N)

    arrayOfchar.reduce((prev,current,index) => {
        for(let i=0; i<D; i++) {
        if(prev===current) {
            arrayOfchar.splice(index, 0, "d")
        }
    }
    })
}

But I don't know hoe should I put the "d" only after two or less consecutive "n"

Anyone clue?

2 Answers 2

2

Rather than creating an entire array of the same character at the very start, I think it would make more sense to create the array piece-by-piece, until N and D come out to 0.

Here's one possible implementation. The general idea is to try to push whichever character count is larger, or if that's not possible due to 3-in-a-row, push the other character, and subtract the appropriate character count by one. Repeat until both counts are 0:

function solution(n, d) {
  const arr = [];
  function canPush(char) {
    const { length } = arr;
    return (arr[length - 1] !== char || arr[length - 2] !== char);
  }
  function push(char) {
    arr.push(char);
    if (char === 'n') n--;
    else if (char === 'd') d--;
  }
  
  while (n > 0 || d > 0) {
    if (n > d) {
      if (canPush('n')) push('n');
      else if (d === 0) return console.log('Impossible');
      else push('d');
    } else if (d >= n) {
      if (canPush('d')) push('d');
      else if (n === 0) return console.log('Impossible');
      else push('n');
    }
  }
  console.log(JSON.stringify(arr));
  // return arr;
}
solution(5, 3);
solution(1, 4);
solution(1, 5);
solution(5, 1);
solution(2, 5);
solution(2, 6);
solution(2, 7);

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

Comments

0

Here is another solution to this interesting problem. The idea is not to go one by one but to figure which one is the larger number and then do an array of pairs of that letter while doing a simple array of the smaller and then just concat them one with another ... so you have 5 and 3 ... nn + d + nn + d + n. 2 pairs of the bigger plus one of the smaller etc.

const fillArray = (length, letter, bigNumber) => {
   var arr = []
   for(var index=0; index < length; index++) {
      arr.push([letter, bigNumber%2 && index+1 === length ? null : letter])
   }
   return arr;
}
const getString = (n, d) => {
  var swtch = d > n, arr = [],
   bigger = {number: swtch ? d : n, letter: swtch ? 'd' : 'n', ceil: Math.ceil((swtch ? d : n)/2)},
   smaller = {number: swtch ? n : d, letter: swtch ? 'n' : 'd', ceil: Math.ceil((swtch ? n : d)/2)}

   if(Math.abs((bigger.number/2) - smaller.number >= 1.5)) {
     return 'Not possible with given parameters!'
   }

   var bigWorkArray = fillArray(bigger.ceil, bigger.letter, bigger.number)
   var smallWorkArray = n === d ? fillArray(smaller.ceil, smaller.letter, smaller.number) : Array(smaller.number).fill(smaller.letter)

   for(var i=0; i < bigWorkArray.length; i++) {
     arr.push(...bigWorkArray[i],...smallWorkArray[i] || '')
   }	
   return arr.join('');
}

console.log(getString(5,3))
console.log(getString(1,4))
console.log(getString(1,5))
console.log(getString(5,1))
console.log(getString(2,5))
console.log(getString(2,6))
console.log(getString(2,7))

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.