Rearrange the characters in a string to generate a new string that returns the number of strings in the newly generated string that do not have consecutive repeating characters. Continuous repetition only takes a single character
For exampleaab
Should return 2 because it has a total of 6 permutations (aab
,aab
,aba
,aba
,baa
,baa
), but only two (aba
andaba
There is no continuous repetition of characters (in this case,a
).
From the online information to get some ideas, my code:
functionPermalone (str) {varArr=str.split (""); varPerarr=[]; varBegin=0; //create a regular, return 0 if the string is fully duplicated varReg =/(.) \1+/G; if(Str.match (REG)!==NULL&&str.match (reg) [0]===str) { return0; } //functions for swapping functionSwap (IDX1,IDX2) {vartemp=ARR[IDX1]; ARR[IDX1]=ARR[IDX2]; ARR[IDX2]=temp; } //If the begin is the last character, you can add the string to the fully arranged array. functionPermall (arr,begin) {if(begin==arr.length-1) {Perarr[perarr.length]=arr.join (""); return; } for(vari=0; (i+begin) <arr.length;i++) {Swap (Begin,begin+i); Permall (Arr,begin+1); Swap (Begin,begin+i); }} permall (Arr,begin); //returns the number of adjacent non-duplicates returnPerarr.filter (function(val) {return!Val.match (REG); }). Length;} Permalone (' AaB ');
First, swap the first character with the character one by one after it.
Next , the first character is fixed, and all subsequent characters are arranged. At this point we are still dividing all the characters in the back into two parts: the first character of the following character, and all the characters after the character. The first character is then exchanged one after the other with the character after it.
The full permutation of the weight is the number from the first number that each number is exchanged with the non-recurring digits behind it.
JS-FCC algorithm-no repeats the full array of strings