How to check if two Strings are anagrams of each other?
A simple coding problem based upon String, but could also be asked with numbers. You need to write a Java program to check if two given strings are anagrams of Each other. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation, and capitalization. Each letter should have the same count in both strings. For example, the Army and Mary are an anagram of each other.
public class Main {
static String one = "seyseysey";
static String two = "yesesyyes";
static char cone[] = one.toCharArray();
static char ctwo[] = two.toCharArray();
static boolean status = false;
public static void main(String[] args) {
for (int i = 0; i < cone.length; i++) {
char check = cone[i];
int number = 0;
for (int j = 0; j < cone.length; j++) {
if (check == cone[j]) {
number++;
}
}
if (stringCheck(check, number) != number) {
status = false;
break;
} else {
status = true;
}
}
System.out.println("Strings are galindrom " + status);
}
public static int stringCheck(char c, int appeared) {
int number = 0;
for (int i = 0; i < ctwo.length; i++) {
if (c == ctwo[i]) {
number++;
}
}
return number;
}
}
public class Main {
static String one = "seyseysey";
static String two = "yesesyyes";
static char cone[] = one.toCharArray();
static char ctwo[] = two.toCharArray();
static boolean status = false;
public static void main(String[] args) {
for (int i = 0; i < cone.length; i++) {
char check = cone[i];
int number = 0;
for (int j = 0; j < cone.length; j++) {
if (check == cone[j]) {
number++;
}
}
if (stringCheck(check, number) != number) {
status = false;
break;
} else {
status = true;
}
}
System.out.println("Strings are galindrom " + status);
}
public static int stringCheck(char c, int appeared) {
int number = 0;
for (int i = 0; i < ctwo.length; i++) {
if (c == ctwo[i]) {
number++;
}
}
return number;
}
}
Comments
Post a Comment