Ask coding questions

← Back to all posts
I am just wondering if my method looks fine to check if the word is anagram or not
ElgunQuliyev (0)

Hello guys. So today I was doing some code challenge and I had this question saying that check if the word is Anagram. I came up with this solutions my own and I was wondering if my method is the right one or not. I am posting the code below please take a look.

public class ifAnagramMyMethod {
    public static void main(String[] args){
        System.out.println(IsAnagram("march","charm"));
    }
    public static boolean IsAnagram(String str, String str2){

        boolean checkIfAnagram = false;

        if(str.length() == str2.length()){
            //I am using loop to see if the str2 has the same letter as str1.If yes return true so it is anagram
            for(int i = 0;i < str.length();i++){

                for(int j = 0;j < str2.length();j++){

                    if(str.charAt(i) == str2.charAt(j)){
                        checkIfAnagram = true;
                    }
                }
            }
        }else{
            checkIfAnagram = false;
        }

        return checkIfAnagram;
    }
}
Comments
hotnewtop
SixBeeps (5047)

Not quite, your method should only really take in one parameter, and you don't need a nested loop either.