javascript--Interview--algorithm--string-3

Source: Internet
Author: User

Problem Description: The simplest string compression is achieved by using the number of occurrences of a character in a string, the string is not shortened after compression, and the original string is returned.

For example Abbbcccccddef, after compression for ab3c5d2ef; The string is large enough (greater than 10 million characters), consider the efficiency problem;

functioncompress_str (str) {varTmp_char = Str.charat (0); varCount = 1; varTmp_str = ' '; varindex = 0;  for(varI=1; i<=str.length; i++){        if(!str.charat (i) | | Str.charat (i)! =Tmp_char) {Tmp_str= Tmp_str + Tmp_char + (Count > 1? Count: "); Tmp_char=Str.charat (i); Count= 1; Index++; } Else{Count++; }    }    if(index = = str.length)returnstr; returntmp_str;}varstr1 = ' ABCDEDF '; Console.log (str1+ ' compressed is ' +compress_str (str1));varstr2 = ' abbbcccccddefbbb '; Console.log (str2+ ' compressed is ' + compress_str (str2));

In JS, once the string has been assigned, it cannot be modified.
Look at the string connection operation based on this background:

var= ' This is a string '= str + ', another string. ';

The processing mechanism for this connection operation JS is: Create a new temporary string, assign the new string to STR + ', another string. ', and then return the new string and destroy the original string at the same time. Therefore, the connection efficiency of the string is low. The way to improve efficiency is to use the Join function of the array

var temparr == ' This is a string '; Temparr.push (SRC); Temparr.push (', another string. '  = Temparr.join (");

However, in the advanced programming of JavaScript, it is also mentioned that:
This process occurs in the background, and this is why it is very slow to stitch strings in some older browsers, such as Firefox, IE6, and so on, which have a version of less than 1.0. But later versions of these browsers have solved this problem.
As a result, efficiency gains are only available in low-version browsers such as IE6.

Therefore, in order to improve efficiency, we need to make further changes, we use string arrays instead of string concatenation

functioncompress_str_2 (str) {varTmp_char = Str.charat (0); varCount = 1; varTmp_arr =NewArray (); varindex = 0;  for(varI=1; i<=str.length; i++){        if(!str.charat (i) | | Str.charat (i)! =Tmp_char) {Tmp_arr.push (Tmp_char)if(Count > 1) Tmp_arr.push (count); Tmp_char=Str.charat (i); Count= 1; Index++; } Else{Count++; }    }    if(index = = str.length)returnstr; returnTmp_arr.join (' ');}varstr1 = ' ABCDEDF '; Console.log (str1+ ' compressed is ' +compress_str_2 (str1));varstr2 = ' abbbcccccddefbbb '; Console.log (str2+ ' compressed is ' + compress_str_2 (str2));

javascript--Interview--algorithm--string-3

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.