same letters

Solutions on MaxInterview for same letters by the best coders in the world

showing results for - "same letters"
Janelle
14 Jun 2020
1String -- Same letters
2Write a return method that check if a string is build out of the same letters as another string.
3Ex:  same("abc",  "cab"); -> true 
4same("abc",  "abb"); -> false:
5 	
6USING CORE JAVA
7Public static void main(String[] args){
8Same(“cab”, “bca”).sout;
9}
10
11public static boolean same(String a, String b) {
12char[] ch1 = a.toCharArray();
13char[]  ch2 = b.toCharArray();
14
15Arrays.sort(ch1);
16Arrays.sort(ch2);
17String a1="", a2="";
18
19for(char each: ch1)
20a1 +=each;
21 
22for(char each: ch2)
23a2 +=each;
24
25return  a1.equals(a2) ; 
26}
27 
28	USING TREE SET
29Public static void main(String[] args){
30Same(“cab”, “bca”).sout;
31}
32public static boolean Same(String str1, String str2) {
33str1 = new TreeSet<String>(Arrays.asList( str1.split("") ) ).toString( );
34str2 = new TreeSet<String>(Arrays.asList( str2.split("") ) ).toString( );
35return  str1.equals(str2);
36}
similar questions
same letters java grepper