Algorithm accumulation (string conversion hump, determine the number of occurrences of the letter in a string, and appear several times)

Source: Internet
Author: User

Because the algorithm is bad, so want to do a bit of this accumulation.

Try to learn a new algorithm every day. (But the estimate is very suspended)

Well, the first one today is the string conversion hump

Directly on the code

var str = ' toupper-case ';     var arr = str.split ('-');  // toupper,case     for (var i = 1; i < arr.length; i++) {   // sets the first value of the array after the first array to uppercase and then the uppercase and the remaining characters of the first character to flatten        arr[i] = Arr[i].charat (0). toUpperCase () + arr[i].slice (1);        };     var str1 = Arr.join (");  // The Join method converts an array into a string    ..... alert (STR1);

The second is to determine the number of occurrences of that letter in a string, and it appears several times.

varstr = ' AAAASSSSDDDDDDD ';varobj = {}; varnum = 0; varValue = ';  for(vari = 0; i < str.length; i++) {        if(!Obj[str[i]]) {Obj[str[i]]= []; } Else{Obj[str[i]].push (str[i]);    }    }; //var obj1 = {    //' a ': [' a '],    //' s ': [' s ', ' s ', ' s ', ' s ', ' s ', ' s '],    //' d ': [' d ', ' d ']    //};     for(varattrinchobj) {        if(Num < Obj[attr].length) {//For the first time A has only one, then num = 1, the second s has n bits, then num = N, and the third d is less than n bits, then num or n                                        //If there is no value greater than num then this value is the maximum number of letters that can be returned to the current num and valuenum =obj[attr].length; Value= Obj[attr][0];    }    }; Alert (Value+ ', ' +num ')

Algorithm accumulation (string conversion hump, determine the number of occurrences of the letter in a string, and appear several times)

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.