JS-FCC algorithm-no repeats the full array of strings

Source: Internet
Author: User
Tags repetition

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 exampleaabShould return 2 because it has a total of 6 permutations (aab,aab,aba,aba,baa,baa), but only two (abaandabaThere 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

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.