The String3 of the Java Programmer's written interview

Source: Internet
Author: User

public class Samestring {    //thought two: Each character corresponds to its own ASC code, the first idea of the algorithm complexity of O (Nlogn), can generally use space to reduce the complexity of time//can open a size of 256 array space, and 256 array elements are set to 0, and then traverse the first string to the character's ASC as an array subscript, the array elements are added 1,//and then traverse the second string to reduce the value of the array element 1, if the last array element value is 0, the description is the same string. The time complexity of this algorithm is O (n) public static void compare (String s1,string s2) {byte[] b1=s1.getbytes (); byte[] B2=s2.getbytes (); int[] Bcount=new int[256];for (int i=0;i<256;i++) {bcount[i]=0;} for (int i=0;i<b1.length;i++) {bcount[b1[i]-' 0 ']++;} for (int i=0;i<b2.length;i++) {bcount[b2[i]-' 0 ']--;} for (int i=0;i<256;i++) if (bcount[i]!=0) {System.out.println ("not equal!"); return;}   System.out.println ("equal!");} public static void Main (string args[]) {string s1= "AAABBBCCC"; String s2= "ABABACBCC"; compare (s1, S2); String s3= "AABBCCDD"; String s4= "abc"; compare (S3, S4);}}
Results: Equalnot Equal


The String3 of the Java Programmer's written interview

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.