Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Compare Two Strings If they are rearranged

I wanted to know how can I compare two rearranged strings E.g if String a="string" ,String b="tsrngi" ... If I compare a.equals(b), It will return false because of order of characters... I want it to return true because characters are same but only order is different.. Thanks

like image 878
user841852 Avatar asked Dec 09 '22 05:12

user841852


1 Answers

Sort them, then compare. To sort, use something like:

char[] content = unsorted.toCharArray();
java.util.Arrays.sort(content);
String sorted = new String(content);
like image 136
JRL Avatar answered Dec 27 '22 06:12

JRL