Repository: TheAlgorithms/Java
Pull request #4175 (2023-04-30 21:50:46)

Added another Optimized solution for Anagrams check.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

/*Respected Maintainer of this Repository, I hope this message finds you well. I am writing to propose another optimized solution for checking if two strings are anagrams or not in Java.
*/

**Approach**
```
boolean isAnagram(String s, String t) {
    if (s.length() != t.length()) {
        return false;
    }
    int[] freq = new int[26];
    for (int i = 0; i < s.length(); i++) {
      int pos1 = s.charAt(i) - 'a';
      int pos2 = s.charAt(i) - 'a';
      freq[pos1]++;  freq[pos2]--;
    }
    for (int i = 0; i < 26; i++) {
        if (freq[i] != 0) {
            return false;
        }
    }
    return true;
}
```


- If the lengths of the two strings are different, they cannot be anagrams. Return false.
- Initialize an array of size 26, where each element corresponds to the frequency of a character.
- Iterate through both strings, incrementing the frequency of each character in the first string and decrementing the frequency of each character in the second string.
- Iterate through the frequency array and check if all the elements are zero. If so, the two strings are anagrams of each other. Return true.
- If any element in the frequency array is non-zero, the two strings are not anagrams of each other. Return false.

**Complexity**

- The time complexity of this approach is O(n), where n is the length of the input strings.
- The space complexity is O(1), as we are using a fixed-size array. This approach is faster than using a hashmap, as it avoids the overhead of creating and iterating through a hashmap.



manojCode94: Added JUnit Test as per suggested for Approach5 in AnagramsTest.java Build successfully.

Patch:
diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java
index 5a9487da..33ea900e 100644
--- a/src/main/java/com/thealgorithms/strings/Anagrams.java
+++ b/src/main/java/com/thealgorithms/strings/Anagrams.java
@@ -43,6 +43,8 @@ public class Anagrams {
          * Auxiliary Space Complexity : O(1)
          * 4th approach Time Complexity : O(n)
          * Auxiliary Space Complexity : O(n)
+         * 5th approach Time Complexity: O(n)
+         * Auxiliary Space Complexity: O(1)
          */
     }
 
@@ -122,4 +124,27 @@ public class Anagrams {
             return nm.equals(kk);
         }
     }
+
+    boolean approach5(String s, String t) {
+        if(s.length() != t.length()){
+            return false;
+        }
+        // Approach is different from above 4 aproaches. 
+        // Here we initialize an array of size 26 where each element corresponds to the frequency of a character. 
+        int[] freq = new int[26];
+        // iterate through both strings, incrementing the frequency of each character in the first string and decrementing the frequency of each character in the second string. 
+        for(int i=0; i<s.length(); i++){
+            int pos1 = s.charAt(i) - 'a';
+			int pos2 = s.charAt(i) - 'a';
+            freq[pos1]++;
+            freq[pos2]--;
+        }
+        // iterate through the frequency array and check if all the elements are zero, if so return true else false
+        for(int i=0; i<26; i++){
+            if(freq[i] != 0){
+                return false;
+            }
+        }
+        return true;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/AnagramsTest.java b/src/test/java/com/thealgorithms/strings/AnagramsTest.java
index 4a0771ee..2b56ef01 100644
--- a/src/test/java/com/thealgorithms/strings/AnagramsTest.java
+++ b/src/test/java/com/thealgorithms/strings/AnagramsTest.java
@@ -18,5 +18,6 @@ public class AnagramsTest {
         assertTrue(anagrams.approach3(input1, "teal"));
         assertTrue(anagrams.approach4(input1, "tale"));
         assertTrue(anagrams.approach4(input1, "teal"));
+        assertTrue(anagrams.approach5(input1, "teal"));
     }
 }