Java Anagrams

  • + 1 comment

    Hello, can someone knowledgable help me understand why this code does not work? It seems replaceFirst(regex, replacement) doesn't work the way I expect it to from https://www.w3schools.com/java/ref_string_replacefirst.asp.

    a = a.toLowerCase();
    b = b.toLowerCase();
            
    if(a.length() != b.length()) { return false; }
            
    for(int i = 0; i < b.length(); i++) {
    	a.replaceFirst(String.valueOf(b.charAt(i)), "");
    	// a.replaceFirst(b.substring(i, i+1), "");
    }
    
    return a.isEmpty();
    

    (I wanted to try to not use Arrays and Hashmaps)