Basic Java Program
An anagram number is a pair of numbers where the digits can be rearranged to form each other.
For instance, if you have the number 1234 and 4321, these two numbers are considered anagrams because you can rearrange the digits of 1234 to get 4321 and vice versa.
Example
- 1234 and 4321: Both numbers contain the same digits (1, 2, 3, 4) but in different orders.
- 356 and 563: Both numbers have the digits 3, 5, and 6, just in different orders.
Non-Examples
- 1234 and 5678: These numbers are not anagrams because they contain different digits.
How to Determine if Two Numbers are Anagrams
- Convert the numbers to strings: This allows you to handle each digit individually.
- Sort the digits: By sorting the digits of both numbers, you can easily compare them.
- Compare the sorted results: If the sorted digits of both numbers match, then the numbers are anagrams of each other.
By following this process, you can check if any two given numbers are anagrams.
import java.util.Arrays;
import java.util.Scanner;
public class AnagramNumber {
// Method to check if two numbers are anagrams
public static boolean areAnagrams(int num1, int num2) {
// Convert numbers to strings
String str1 = Integer.toString(num1);
String str2 = Integer.toString(num2);
// Check if lengths are different
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to character arrays
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
// Sort character arrays
Arrays.sort(arr1);
Arrays.sort(arr2);
// Compare sorted arrays
return Arrays.equals(arr1, arr2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st Number: ");
int num1 = sc.nextInt();
System.out.println("Enter the 2nd Number: ");
int num2 = sc.nextInt();
if (areAnagrams(num1, num2)) {
System.out.println(num1 + " and " + num2 + " are anagrams.");
} else {
System.out.println(num1 + " and " + num2 + " are not anagrams.");
}
}
}
/* Output:
Enter the 1st Number:
3214
Enter the 2nd Number:
4123
3214 and 4123 are anagrams. */
Let’s break down the areAnagrams
method:
Purpose
The method public static boolean areAnagrams(int num1, int num2)
checks if two integer numbers (num1
and num2
) are anagrams of each other. Two numbers are anagrams if they have the same digits in different orders.
Code Explanation
- Convert Numbers to Strings
String str1 = Integer.toString(num1);
String str2 = Integer.toString(num2);
- The
Integer.toString(num1)
method converts the integernum1
into its string representation. - Similarly,
Integer.toString(num2)
convertsnum2
to a string. - This conversion allows us to work with the individual digits of the numbers as characters.
2. Check if Lengths are Different
if (str1.length() != str2.length()) {
return false;
}
- This step compares the lengths of the two string representations.
- If the lengths are different, the numbers cannot be anagrams, so the method immediately returns
false
.
3. Convert Strings to Character Arrays
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
- The
toCharArray()
method converts the stringsstr1
andstr2
into arrays of characters. - This allows us to manipulate and sort the individual digits of the numbers.
4. Sort Character Arrays
Arrays.sort(arr1);
Arrays.sort(arr2);
- The
Arrays.sort()
method sorts the character arraysarr1
andarr2
in ascending order. - Sorting is used so that we can easily compare the characters of the two numbers. If the arrays are anagrams, the sorted arrays will be identical.
5. Compare Sorted Arrays
return Arrays.equals(arr1, arr2);
- The
Arrays.equals()
method compares the sorted arraysarr1
andarr2
. - If the sorted arrays are identical, the method returns
true
, indicating that the original numbers are anagrams. Otherwise, it returnsfalse
.
In Short,
- The method converts the numbers to strings and then to character arrays.
- It checks if the lengths are equal.
- It sorts the character arrays and compares them.
- If the sorted arrays are the same, the numbers are anagrams; otherwise, they are not.
This approach ensures that you are correctly identifying whether two numbers have the same digits in different orders.
Check whether two Strings are anagram of each other
An anagram string is a string that can be rearranged to form another string by using the exact same characters, each character appearing the same number of times.
In other words, two strings are anagrams if you can rearrange the letters of one string to match the other.
Examples:
- “listen” and “silent”
- “evil” and “vile”
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// take input from users
System.out.print("Enter first String: ");
String str1 = input.nextLine();
System.out.print("Enter second String: ");
String str2 = input.nextLine();
// check if length is same
if(str1.length() == str2.length()) {
// convert strings to char array
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
// sort the char array
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// if sorted char arrays are same
// then the string is anagram
boolean result = Arrays.equals(charArray1, charArray2);
if(result) {
System.out.println(str1 + " and " + str2 + " are anagram.");
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
input.close();
}
}
/* Output:
Enter first String: listen
Enter second String: silent
listen and silent are anagram. */
Overview
This program checks if two user-inputted strings are anagrams of each other. It uses sorting and comparison of character arrays to determine if the strings contain the same characters in different orders.
Code Explanation
- Imports
import java.util.Arrays;
import java.util.Scanner;
java.util.Arrays
is used for sorting and comparing arrays.java.util.Scanner
is used for taking user input.
2. Main Class and Method
class Main {
public static void main(String[] args) {
- Defines the
Main
class with amain
method, which is the entry point of the program.
3. Creating Scanner Object
Scanner input = new Scanner(System.in);
- Creates a
Scanner
object namedinput
to read user input from the console.
4. Reading User Input
System.out.print("Enter first String: ");
String str1 = input.nextLine();
System.out.print("Enter second String: ");
String str2 = input.nextLine();
- Prompts the user to enter two strings and stores them in
str1
andstr2
.
5. Checking String Length
if(str1.length() == str2.length()) {
- Checks if the lengths of the two strings are equal. If not, the strings cannot be anagrams.
6. Converting Strings to Character Arrays
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
- Converts each string to a character array.
7. Sorting Character Arrays
Arrays.sort(charArray1);
Arrays.sort(charArray2);
- Sorts the character arrays. Sorting makes it easy to compare the characters.
8. Comparing Sorted Arrays
boolean result = Arrays.equals(charArray1, charArray2);
- Compares the sorted character arrays. If they are identical, the strings are anagrams.
9. Displaying Results
if(result) {
System.out.println(str1 + " and " + str2 + " are anagram.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
- Prints whether the strings are anagrams based on the comparison result.
10. Closing Scanner
input.close();
- Closes the
Scanner
object to release system resources.
Sample Input and Output
Example 1
Input:
Enter first String: listen
Enter second String: silent
Explanation:
- Both strings have the same length (6 characters).
Convert both strings to character arrays:
listen
becomes['l', 'i', 's', 't', 'e', 'n']
silent
becomes['s', 'i', 'l', 'e', 'n', 't']
Sort the character arrays:
['e', 'i', 'l', 'n', 's', 't']
['e', 'i', 'l', 'n', 's', 't']
Both sorted arrays are identical, so listen
and silent
are anagrams.
Output:
listen and silent are anagram.
Summary
The program:
- Reads two strings from the user.
- Checks if they have the same length.
- Converts them to character arrays, sorts these arrays, and compares them.
- Determines if the strings are anagrams and prints the result.