Js-FCC algorithm-full arrangement of No repeats please strings (detailed description), js-fcc-no
Rearranges the characters in a string to generate a new string, and returns the number of strings in the new string that do not contain consecutive repeated characters. continuous repetition only takes a single character as the standard.
For example, aab should return 2 because it has 6 in total (aab, aab, aba, aba, baa, baa), but only two (aba and aba) there are no consecutive repeated characters (in this example, it is ).
I got some ideas from the online materials, my code:
Function permAlone (str) {var arr = str. split (""); var perarr = []; var begin = 0; // create a regular expression. If all strings are repeated, return 0 var reg =/(.) directly /(.) \ 1 +/g; if (str. match (reg )! = Null & str. match (reg) [0] ===str) {return 0 ;}// function swap (idx1, idx2) used for switching {var temp = arr [idx1]; arr [idx1] = arr [idx2]; arr [idx2] = temp;} // If begin reaches the last character, function permall (arr, begin) {if (begin = arr. length-1) {perarr [perarr. length] = arr. join (""); return;} for (var I = 0; (I + begin) <arr. length; I ++) {swap (begin, begin + I); permall (arr, begin + 1); swap (begin, begin + I);} permall (arr, begi N); // return the number of adjacent non-repeated return perarr. filter (function (val) {return! Val. match (reg) ;}). length ;}permalone ('aab ');
First, the first character and the subsequent characters are exchanged one by one.
Then, fix the first character and find the arrangement of all subsequent characters. At this time, we still divide all the following characters into two parts: the first character after the character, and all the characters after the character. Then, the first character is exchanged with the subsequent characters one by one.
The fully arranged deduplication refers to the exchange of each number from the first number with a non-repeated number next to it.
The above js-FCC algorithm-No repeats please string full arrangement (detailed explanation) is all the content that I have shared with you. I hope you can give me a reference, we also hope that you can support the customer's home.