bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Check Anagram Strings

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Check Anagram Strings?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.Arrays;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Explanation: We convert both strings to arrays, sort them, and check if they are equal.
Two strings are anagrams if they contain the same characters in a different order:
Check Anagram Strings

Check Anagram Strings

Two strings are anagrams if they contain the same characters in a different order:

Example

import java.util.Arrays;
String str1 = "listen";
String str2 = "silent";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
boolean isAnagram = Arrays.equals(arr1, arr2);
System.out.println(isAnagram ? "Anagram" : "Not Anagram");

Explanation: We convert both strings to arrays, sort them, and check if they are equal. If yes, the strings are anagrams.

Previous

Java How To - Palindrome Check

Next

Java How To Convert a String to an Array