I have to create a Java program that returns true
if two strings are composed by the same letters like "EMS"
and "MES"
, or false
if it is not the case.
I tried to develop some code and I finally found a solution but It only works for words which have all letters different (no letters duplicated) It doesn't work for "EEM"
and "EMS"
.
String mot = "EMS", word="EES";
char[] tab= mot.toCharArray();
char[] vect= word.toCharArray();
int i = mot.length(), j = word.length(), a = 0;
if (i != j)
System.out.println("false");
else {
for (int k=0; k<i; k++) {
for (int l=0; l<i; l++) {
if (tab[k] == vect[l])
a++;
}
}
if (a == i)
System.out.println("true");
else
System.out.println("false");
}