이 기사에서는 두 문자열이 아나그램인지 확인하는 방법을 이해합니다. 아나그램은 다른 단어의 문자를 재배열하여 형성된 단어 또는 구입니다. 사용자가 두 개의 문자열을 입력합니다. 각 문자('a'에서 'z'까지)가 몇 번 나타나는지 계산한 다음 해당 개수를 비교해야 합니다. 문자열에서 알파벳의 빈도는 문자열에 나타나는 횟수입니다. 두 문자열의 특정 알파벳 빈도수가 같으면 이 두 문자열을 아나그램이라고 할 수 있습니다.
아래는 동일한 데모입니다 -
입력
입력이 -
라고 가정합니다.Enter the first string : Race Enter the second string : Care
출력
원하는 출력은 -
The strings race and care are anagram.
알고리즘
Step 1 - START Step 2 - Declare two string values namely my_string_1, my_string_2 Step 3 - Read the required values from the user/ define the values Step 4 - Convert both the strings to lower case letters using toLowerCase() function Step 5 - Check if the length of the two strings are same, if not, they are not anagram strings. Step 6 - Assign the strings to character arrays and sort them. Step 7 - Use the function ‘equals()’ to check if they are equal. If yes, they are anagram strings, else they are not anagram strings. Step 8 - Display the result Step 9 - Stop
예시 1
여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 코딩 기반 도구에서 이 예제를 라이브로 사용해 볼 수 있습니다.
.
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
System.out.println("Required packages have been imported");
String my_string_1, my_string_2;
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the first string : ");
my_string_1 = my_scanner.nextLine();
System.out.print("Enter the second string : ");
my_string_2 = my_scanner.nextLine();
my_string_1 = my_string_1.toLowerCase();
my_string_2 = my_string_2.toLowerCase();
if(my_string_1.length() == my_string_2.length()) {
char[] my_array_1 = my_string_1.toCharArray();
char[] my_array_2 = my_string_2.toCharArray();
Arrays.sort(my_array_1);
Arrays.sort(my_array_2);
boolean my_result = Arrays.equals(my_array_1, my_array_2);
if(my_result) {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are anagram.");
} else {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
}
} else {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
}
}
} 출력
Required packages have been imported A reader object has been defined Enter the first string : Race Enter the second string : Care The strings race and care are anagram.
예시 2
여기서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
System.out.println("Required packages have been imported");
String my_string_1, my_string_2;
my_string_1 = "Race";
my_string_2 = "Care";
System.out.println("The two strings are defined as " +my_string_1 +" and " + my_string_2);
my_string_1 = my_string_1.toLowerCase();
my_string_2 = my_string_2.toLowerCase();
if(my_string_1.length() == my_string_2.length()) {
char[] my_array_1 = my_string_1.toCharArray();
char[] my_array_2 = my_string_2.toCharArray();
Arrays.sort(my_array_1);
Arrays.sort(my_array_2);
boolean my_result = Arrays.equals(my_array_1, my_array_2);
if(my_result) {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are anagram.");
} else {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
}
} else {
System.out.println("The strings "+my_string_1 + " and " + my_string_2 + " are not anagram.");
}
}
} 출력
Required packages have been imported The two strings are defined as Race and Care The strings race and care are anagram.