Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringAnagram Check #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Java/StringAnagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Arrays;

class Main {
public static void main(String[] args) {
String str1 = "Race";
String str2 = "Care";

str1 = str1.toLowerCase();
str2 = str2.toLowerCase();

// 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.");
}
}
}