javascript--Interview--algorithm--string-1

Source: Internet
Author: User

/**

Description
* Implements an algorithm that determines whether all characters of a string are all different.
* What if you do not allow the use of additional data structures?
* Details: 1) Unicode character set or ASCII character set
* 2) If the ASCII encoding, the length is greater than 256, the direct return false
*/

 functionno_multiple_str (str) {if(Str.length > 256)return false; varTMP = {};  for(vari=0; i<str.length; i++){         var Char=Str.charat (i); if(tmp[Char]) {             return false; } Else{tmp[Char] =Char; }     }     return true; } varstr = "Rept"; Console.log (' No multiple char--and ' + NO_MULTIPLE_STR (str). toString ());

/**
* Use bit arithmetic to reduce space complexity
*/

functionno_multiple_str_2 (str) {if(Str.length > 256)return false; varChecker = 0;  for(vari=0; i<str.length; i++){         varValue = Str.charcodeat (i)-' a '. charCodeAt (); Console.log (Checker& (1 <<value)); if((Checker & (1 << value)) > 0) {             return false; } Checker|= (1 <<value); }     return true;}varstr = "Repte"; Console.log (' No multiple char--and ' + no_multiple_str_2 (str). toString ());

/**

* The most basic method
* Cycle Comparison
*/

functionno_multiple_str_3 (str) {if(Str.length > 256)return false;  for(vari=0; i<str.length-1; i++){          for(varj=i+1; j<str.length; J + +) {             if(Str.charat (i) = =Str.charat (j)) {                 return false; }         }     }     return true;} varstr = "Rept"; Console.log (' No multiple char--and ' + no_multiple_str_3 (str). toString ());

javascript--Interview--algorithm--string-1

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.