I am having some trouble when I try to compare two strings. My first string is a word and my second string are some letters that form my word, or not, for example:
String 1, my word: "test"
String 2, my soup: "adhesljdtth"
In this case, I got all the characters of both strings, and start to process them, when I found some char that belongs to my word in my soup, I need to remove it from my soup, and go to the next element.
I found some ways to compare it and get the results using: StringBuilder, LinkedList, arrays and so on, all work with small strings, but when I get a string with a million of characters, I got some performance problems. I tried to use Binary Search in this case, but even this is taking so long to process my results.
I am using Array.sort function to sort both of my strings.
And to verify if soup has all the letters to form my word, I am doing this:
for (int i = 0; i < wordLenght; i++) {
char key = wordCharList[i];
int length = soupCharList.size();
int low = 0;
int high = length - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
char midVal = soupCharList.get(mid);
if (midVal < key) {
low = mid + 1;
}
else if (midVal > key) {
high = mid - 1;
}
else if(midVal == key) {
soupCharList.remove(mid);
break;
}
if(high == -1) {
return false;
}
}
}
return true;
}
Do you have any ideas how to compare it with a better performance?