Js-FCC algorithm-full arrangement of No repeats please strings (detailed description), js-fcc-no

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.