Given two strings, determine whether the characters of one of the strings can be changed to another string after being rearranged.

Source: Internet
Author: User
1/* 2 * given two strings, determine whether the characters of one of the strings can be changed to another after being rearranged, in fact, that is, the problem of the displacement word 3 * For example, A = 'abc' B = 'acb' can be changed to B through the 4 * idea of a; 5*1. should the analysis be conducted. Are the reserved words case sensitive? For example, is God and dog an immutable word? Do you still need to consider space issues? 6 * Here we assume it is case sensitive. 7*2. when comparing two strings, if their lengths are not equal, it is definitely not an immutator 8*9*10 */11 12 public class issamestring {13 14 public static void main (string [] ARGs) {15 // todo auto-generated method stub16 string S = "ABCD"; 17 string T = "dcba"; 18 issamestring ISS = new issamestring (); 19 Boolean flag = ISS. permutation (S, T); 20 if (FLAG) 21 system. out. println ("displacement"); 22 else23 system. out. println ("not modified words"); 24 25} 26/* 27 * if the two are modified words, they all have the same Characters, but the order is different. If 28 * is used to sort them, returns the same string Order 29 *. You can use this to determine 30*31 */32 Public String sort (string s) 33 {34 char [] content = S. tochararray (); 35 Java. util. arrays. sort (content); 36 return new string (content); 37} 38 public Boolean permutation (string S, string t) 39 {40 if (S. length ()! = T. Length () 41 return false; 42 return sort (s). Equals (sort (t); 43} 44 45 46 47}

 

Given two strings, determine whether the characters of one of the strings can be changed to another string after being rearranged.

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.