Record the problem with string handling

Source: Internet
Author: User

Question one: Find out if two strings of string have the same character.

  Idea one: Similar to two sets to find the same elements, a set as a benchmark to facilitate two sets, encountered the same element is interrupted, the time cost is O (N2) square level, inefficient, but also the most easy to think of the method.

public Boolean StringSearch1 (String s) {Boolean same=false;char[] chset=new char[s.length ()];s.getchars (0, S.length (), Chset, 0); for (int i=0;i<chset.length;i++) {boolean bsignal=false;for (int j=0;j<chset.length;j++) {if (j!=i) if ( Chset[i]==chset[j]) {same=true;bsignal=true;break;}} if (bsignal) break;} return same;}

idea two: Use Java map map to store characters, encounter the same key value compare value, and set to the same after exiting. Map maps are the same as the idea of a hash table, where space is exchanged for time to achieve O (n) efficiency, but another space for O (n) is needed to store it. This is a good way to do this, but as the amount of data increases, it can result in a huge performance loss for the storage service.

public Boolean STRINGSEARCH2 (String s) {Boolean same=false; Map<character, boolean> map=new hashmap<character, boolean> (); for (int i=0;i<s.length (); i++) {if ( Map.get (S.charat (i)) ==null) Map.put (S.charat (i), true); else{same=true;break;}} return same;}

idea Three: we learned that there is a way to simplify the storage of information by the information fingerprint, and understand that the information fingerprint can be used to identify a single information, and the computed fingerprint repetition rate is very low. Through this thought, we think of each character in the system there is an ASCII code, they are not the same number of an integer (also can be said to be a binary number), as a basis to establish a a-z,a-z dictionary byte array, and then hash the way to store and find the same element. (Further optimization is a bit mapping, a byte=8 bit for 1 to 8 storage). Efficiency can reach O (n).

A-Z 65-90 A-Z 97-122public boolean STRINGSEARCH3 (String s) {Boolean same=false;byte[] map =new byte[52];for (int i=0;i< ; map.length;i++) map[i]=0;for (int i=0;i<s.length (); i++) {char c=s.charat (i); int code= (int) c;if (code<=90 & & code>=65) {if (map[code-65]==1) {same=true;break;} Elsemap[code-65]=1;} else if (Code >=97 && code <=122) {if (map[code-97+26]==1) {same=true;break;} Elsemap[code-97+26]=1;}} return same;}

Issue two: Implementing string inversion

  I did not think of a better way to do this than to create a new string of equal length and dump the bits.

Public String StringReserve1 (string s) throws Unsupportedencodingexception{int Length=s.length (); byte[] B1=s.getbytes (); byte[] b2=new byte[length];for (int i=0;i<length;i++) {b2[i]=b1[length-i-1];} String Res=new string (B2, "Utf-8"); return ReS;}

int lengthofstr (const char *c) {    const char *p=c;    int len=0;    while (*p!= ') {        len++;        p++;    }        return Len;} const char * REVERSESTR (const char *c) {    char* reverstr;    int Len=lengthofstr (c);    Reverstr=new char (len);    for (int i=0;i<len;i++)        * (reverstr+i) =* (c+len-1-i);    return REVERSTR;}

Record the problem with string handling

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.