SoFunction
Updated on 2025-02-28

js-FCC algorithm - full arrangement of No repeats please strings (detailed explanation)

Rearrange the characters in a string to generate a new string, and return the number of strings in the newly generated string with no continuous repeating characters. Continuous repeating is only a single character

For example, aab should return 2 because it has 6 in total (aab, aab, aba, aba, baa, baa), but only two (aba and aba) without consecutive duplicate characters (in this case a).

I got some ideas from online information, my code:

function permAlone(str) {
 var arr=("");
 var perarr=[];
 var begin=0;
 //Create a regularity, if the string is completely repeated, return 0 directly var reg = /(.)\1+/g;
 if((reg)!==null&&(reg)[0]===str){
  return 0;
 }
 // Functions for exchange function swap(idx1,idx2){
   var temp=arr[idx1];
   arr[idx1]=arr[idx2];
   arr[idx2]=temp;
 }
 //If begin to the last character, you can add this string to the fully arranged array. function permall(arr,begin){
  if(begin==-1){
   perarr[]=("");
   return;
  }
  for(var i=0;(i+begin)<;i++){
   swap(begin,begin+i);
   permall(arr,begin+1);
   swap(begin,begin+i);
  }
 }
 permall(arr,begin);
 //Return the number of adjacent non-repetition return (function(val) {
   return !(reg);
 }).length;
}

permAlone('aab');

First, exchange the first character and the characters after it one by one.

Next, fix the first character and find the arrangement of all the following characters. At this time, we still divide all the characters afterwards into two parts: the first character of the character afterwards, and all the characters after this character. Then exchange the first character with the characters after it one by one.

The complete arrangement of deduplication is to exchange each number with the non-repetitive number after it from the first number.

The above article "The full arrangement of the js-FCC algorithm-No repeats please string (detailed explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.