Hi, I'm having trouble getting my anagram method to work. I have to solve it recursively. The explanation of the problem is in the comment section in the beginning of my code. I can't figure out why it reads false for everything I try to test it with. Can anyone help me point out my mistake? Any guidance or advice would be helpful. Thank you.
Please use String.valueOf(s1.charAt(i)) instead. The code you used will use that call in the background, but will also create a StringBuilder:
By using String.valueOf directly you will save that one object. Maybe this is micro-optimization, but using these little API calls is still a good idea.
Just one other question: why do you need your call to be recursive? I can find one way that runs not in O(m * n) with m being the length of the first string and n being the length of the second string*, but in O(k log k) with k being the maximum of m and n. It involves converting the two arrays into character arrays (O(k)), sorting these (O(k log k)), then comparing the two arrays for equality (O(k)). All you will then need is a handful of API calls, most from String, the others from java.util.Arrays. The complete program will run with just 9 method calls, 4 of which are your calls for changing the strings into lower case and removing spaces.
* In your code you loop over m, inside that loop you loop over n.