The String2 of the Java Programmer's written interview

Source: Internet
Author: User

2. How to determine whether 2 strings have the same character composition such as "AAAABBC" and "ABCBAAA"

2 Kinds of ideas


Import Java.lang.reflect.array;import java.util.arrays;//thought one: First sort 2 strings, in comparison, the so-called 2 string equal refers to the number of letters and numbers that make up a string, The order can be different public class Thesamestring {public static void compare (String s1,string s2) {byte[] b1=s1.getbytes (); byte[] B2= S2.getbytes (); Arrays.sort (B1); Arrays.sort (B2); S1=new string (B1); S2=new string (B2); if (s1.equals (s2) ==true) {System.out.println ("equal");} ELSE{SYSTEM.OUT.PRINTLN ("Not 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




String2 of written interview for Java programmer

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.