From 38daad8fa98615b8a6e686885a0a4a6452f59543 Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Sun, 5 Dec 2021 20:16:29 +0530 Subject: [PATCH 1/3] Fixes: #{2391} --- .../com/thealgorithms/strings/Anagrams.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/Anagrams.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java new file mode 100644 index 000000000000..1db3bf1b7bf2 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -0,0 +1,150 @@ +/** Author : Siddhant Swarup Mallick + * Github : https://github.com/siddhant2002 + */ + +/** PROBLEM DESCRIPTION : + * An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode. Reference from https://en.wikipedia.org/wiki/Anagram + */ + +package com.thealgorithms.strings; +import java.util.*; +public class Anagrams +{ + // 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time. + public static void main(String args[]) { + String first = "deal"; + String second = "lead"; + // All the below methods takes input but doesn't return any output to the main method. + Anagrams nm=new Anagrams(); + nm.approach2(first, second); /* To activate methods for different approaches*/ + nm.approach1(first, second); /* To activate methods for different approaches*/ + nm.approach3(first, second); /* To activate methods for different approaches*/ + nm.approach4(first, second); /* To activate methods for different approaches*/ + + /** + * OUTPUT : + * first string ="deal" second string ="lead" + * Output: Anagram + * Input and output is constant for all four approaches + * 1st approach Time Complexity : O(n logn) + * Auxiliary Space Complexity : O(1) + * 2nd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 3rd approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * 4th approach Time Complexity : O(n) + * Auxiliary Space Complexity : O(n) + */ + } + + void approach1(String s, String t) + { + if (s.length() != t.length()) + { + System.out.println(false); + } + else + { + char c[] = s.toCharArray(); + char d[] = t.toCharArray(); + Arrays.sort(c); + Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ + if (Arrays.equals(c, d)) + { + System.out.println("Anagram"); + } else + { + System.out.println("Not Anagram"); + } + } + } + + void approach2(String a, String b) + { + if(a.length()!=b.length()) + { + System.out.println("Not Anagram"); + } + else + { + int m[]=new int[26]; + int n[]=new int[26]; + for(char c: a.toCharArray()) + { + m[c-'a']++; + } + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // Running time and space complexity of this algo is less as compared to others + for(char c:b.toCharArray()) + { + n[c-'a']++; + } + for(int i=0;i<26;i++) + { + if(m[i]!=n[i]) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } + + void approach3(String s, String t) + { + if(s.length()!=t.length()) + { + System.out.println("Not Anagram"); + } + // this is similar to approach number 2 but here the string is not converted to character array + else + { + int a[]=new int[26]; + int b[]=new int[26]; + int k=s.length(); + for(int i=0;i nm=new HashMap<>(); + HashMap kk=new HashMap<>(); + for(char c: s.toCharArray()) + { + nm.put(c, nm.getOrDefault(c,0)+1); + } + for(char c: t.toCharArray()) + { + + kk.put(c, kk.getOrDefault(c,0)+1); + } + // It checks for equal frequencies + for(char c:nm.keySet()) + { + if(!nm.get(c).equals(kk.get(c))) + { + System.out.println("Not Anagram"); + } + } + System.out.println("Anagram"); + } + } +} \ No newline at end of file From a4172291f5745bd11c02580f9468225edf07bf2c Mon Sep 17 00:00:00 2001 From: siddhant2002 Date: Tue, 7 Dec 2021 18:21:15 +0530 Subject: [PATCH 2/3] Fixes #(2391) --- .../com/thealgorithms/strings/Anagrams.java | 44 +++++++++---------- .../strings/String_Comparision.java | 0 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java index 1db3bf1b7bf2..f76db06c4d58 100644 --- a/src/main/java/com/thealgorithms/strings/Anagrams.java +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -16,10 +16,10 @@ public static void main(String args[]) { String second = "lead"; // All the below methods takes input but doesn't return any output to the main method. Anagrams nm=new Anagrams(); - nm.approach2(first, second); /* To activate methods for different approaches*/ - nm.approach1(first, second); /* To activate methods for different approaches*/ - nm.approach3(first, second); /* To activate methods for different approaches*/ - nm.approach4(first, second); /* To activate methods for different approaches*/ + System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/ + System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/ /** * OUTPUT : @@ -37,11 +37,11 @@ public static void main(String args[]) { */ } - void approach1(String s, String t) + boolean approach1(String s, String t) { if (s.length() != t.length()) { - System.out.println(false); + return false; } else { @@ -51,19 +51,19 @@ void approach1(String s, String t) Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */ if (Arrays.equals(c, d)) { - System.out.println("Anagram"); + return true; } else { - System.out.println("Not Anagram"); + return false; } } } - void approach2(String a, String b) + boolean approach2(String a, String b) { if(a.length()!=b.length()) { - System.out.println("Not Anagram"); + return false; } else { @@ -73,7 +73,7 @@ void approach2(String a, String b) { m[c-'a']++; } - // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed + // In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed in the form of boolean format // Running time and space complexity of this algo is less as compared to others for(char c:b.toCharArray()) { @@ -83,18 +83,18 @@ void approach2(String a, String b) { if(m[i]!=n[i]) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } - void approach3(String s, String t) + boolean approach3(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } // this is similar to approach number 2 but here the string is not converted to character array else @@ -110,19 +110,19 @@ void approach3(String s, String t) for(int i=0;i<26;i++) { if(a[i]!=b[i]) - System.out.println("Not Anagram"); + return false; } - System.out.println("Anagram"); + return true; } } - void approach4(String s, String t) + boolean approach4(String s, String t) { if(s.length()!=t.length()) { - System.out.println("Not Anagram"); + return false; } - // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed + // This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed in boolean format else { HashMap nm=new HashMap<>(); @@ -141,10 +141,10 @@ void approach4(String s, String t) { if(!nm.get(c).equals(kk.get(c))) { - System.out.println("Not Anagram"); + return false; } } - System.out.println("Anagram"); + return true; } } } \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java new file mode 100644 index 000000000000..e69de29bb2d1 From e3c28dc4a8b56687faf4d82952bb5e881b2a689b Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Thu, 9 Dec 2021 20:45:40 +0200 Subject: [PATCH 3/3] Delete String_Comparision.java --- src/main/java/com/thealgorithms/strings/String_Comparision.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/strings/String_Comparision.java diff --git a/src/main/java/com/thealgorithms/strings/String_Comparision.java b/src/main/java/com/thealgorithms/strings/String_Comparision.java deleted file mode 100644 index e69de29bb2d1..000000000000