ในบทความนี้ เราจะเข้าใจวิธีการตรวจสอบว่าสองสตริงเป็นแอนนาแกรมหรือไม่ แอนนาแกรมคือคำหรือวลีที่เกิดจากการจัดเรียงตัวอักษรของคำอื่น ผู้ใช้ป้อนสองสตริง เราต้องนับจำนวนครั้งที่แต่ละตัวอักษร ('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.