minimumMoves

by shan10213223

JavaScript

function minimumMoves(words) {
	const res = [];
  
  for (let word of words) {
  	console.log('word: ', word);
  	let total = 0;
  	let count = 0;
  	for (let i = 1; i < word.length; i++) {
    	if (word[i] === word[i-1]) {
      	if (count === 0) {
        	count++
        }
        count++
        if (i === word.length - 1) {
        	total += Math.floor(count/2);
        }
      } else {
      	if (count > 0) {
        	total += Math.floor(count/2);
          
        }
        count = 0;
      }
    }
    res.push(total);
  }
  
	return res;
}

const test1 = ['ab', 'aab', 'abb', 'abc', 'abbba'];
console.log(minimumMoves(test1));