|
| 1 | +# *30. Anagram* |
| 2 | +The problem can be found at the following link: [Problem Link](https://www.geeksforgeeks.org/problems/anagram-1587115620/1) |
| 3 | + |
| 4 | +<div align="center"> |
| 5 | + <h2>✨ LeetCode Problem of the Day (POTD) Started ✨</h2> |
| 6 | +</div> |
| 7 | + |
| 8 | +- As promised in the poll, I’ve started solving and uploading **LeetCode Problem of the Day (POTD)** solutions! 🎯 |
| 9 | +- My solution for today's problem is now live: |
| 10 | + **[2097. Valid Arrangement of Pairs](https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/2097.Valid%20Arrangement%20of%20Pairs.md)** |
| 11 | + |
| 12 | +<div align="center"> |
| 13 | + <a href="https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/2097.Valid%20Arrangement%20of%20Pairs.md"> |
| 14 | + <img src="https://img.shields.io/badge/LeetCode%20POTD-Solution%20Live-brightgreen?style=for-the-badge&logo=leetcode" alt="LeetCode POTD Solution" /> |
| 15 | + </a> |
| 16 | + <a href="https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/2097.Valid%20Arrangement%20of%20Pairs.md"> |
| 17 | + <img src="https://img.shields.io/badge/Solutions-Up%20to%20Date-blue?style=for-the-badge" alt="Solutions Up-to-Date" /> |
| 18 | +</div> |
| 19 | + |
| 20 | +<br/> |
| 21 | + |
| 22 | +## Problem Description |
| 23 | + |
| 24 | +You are given two strings `s1` and `s2`, both consisting of lowercase alphabets. Your task is to check whether the two strings are anagrams of each other. Two strings are anagrams if they contain the same characters, but the order of characters may differ. |
| 25 | + |
| 26 | +### Examples: |
| 27 | + |
| 28 | +**Input:** |
| 29 | +`s1 = "geeks", s2 = "kseeg"` |
| 30 | +**Output:** |
| 31 | +`true` |
| 32 | + |
| 33 | +**Explanation:** |
| 34 | +Both strings contain the same characters with the same frequency, so they are anagrams. |
| 35 | + |
| 36 | +**Input:** |
| 37 | +`s1 = "allergy", s2 = "allergic"` |
| 38 | +**Output:** |
| 39 | +`false` |
| 40 | + |
| 41 | +**Explanation:** |
| 42 | +The characters in both strings are not the same, so they are not anagrams. |
| 43 | + |
| 44 | +**Input:** |
| 45 | +`s1 = "g", s2 = "g"` |
| 46 | +**Output:** |
| 47 | +`true` |
| 48 | + |
| 49 | +**Explanation:** |
| 50 | +Both strings contain the same character, so they are anagrams. |
| 51 | + |
| 52 | +### Constraints: |
| 53 | +- `1 ≤ s1.size(), s2.size() ≤ 10^5` |
| 54 | + |
| 55 | +## My Approach |
| 56 | + |
| 57 | +1. **Character Counting**: |
| 58 | + - Since anagrams contain the same characters with the same frequency, we can compare the frequency of characters in both strings. |
| 59 | + - To do this efficiently, we use an array of size 26 (for the 26 lowercase letters) to count the frequency of characters in both strings. |
| 60 | + - The idea is to increment the count for characters in `s1` and decrement the count for characters in `s2`. If the counts match after processing both strings, the strings are anagrams. |
| 61 | + |
| 62 | +2. **Steps:** |
| 63 | + - Check if both strings have the same length. If not, they cannot be anagrams. |
| 64 | + - Iterate through the strings and update the frequency count for each character. |
| 65 | + - After processing both strings, check if all the counts are zero. If they are, return `true`; otherwise, return `false`. |
| 66 | + |
| 67 | +## Time and Auxiliary Space Complexity |
| 68 | + |
| 69 | +- **Expected Time Complexity:** O(n), where `n` is the length of the strings. We traverse both strings once, performing constant-time operations for each character. |
| 70 | +- **Expected Auxiliary Space Complexity:** O(1), as we only use a constant amount of additional space for the frequency count array (size 26). |
| 71 | + |
| 72 | +## Code (C) |
| 73 | + |
| 74 | +```c |
| 75 | +bool areAnagrams(char s1[], char s2[]) { |
| 76 | + if (strlen(s1) != strlen(s2)) return false; |
| 77 | + |
| 78 | + int counts[26] = {0}; |
| 79 | + for (int i = 0; i < strlen(s1); i++) { |
| 80 | + counts[s1[i] - 'a']++; |
| 81 | + counts[s2[i] - 'a']--; |
| 82 | + } |
| 83 | + |
| 84 | + for (int i = 0; i < 26; i++) { |
| 85 | + if (counts[i] != 0) return false; |
| 86 | + } |
| 87 | + |
| 88 | + return true; |
| 89 | +} |
| 90 | +``` |
| 91 | +
|
| 92 | +## Code (Cpp) |
| 93 | +
|
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + bool areAnagrams(string& s1, string& s2) { |
| 98 | + if (s1.length() != s2.length()) return false; |
| 99 | + int counts[26] = {0}; |
| 100 | + for (int i = 0; i < s1.length(); i++) { |
| 101 | + counts[s1[i] - 'a']++; |
| 102 | + counts[s2[i] - 'a']--; |
| 103 | + } |
| 104 | + for (int count : counts) { |
| 105 | + if (count != 0) return false; |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +## Code (Java) |
| 113 | + |
| 114 | +```java |
| 115 | +class Solution { |
| 116 | + public static boolean areAnagrams(String s1, String s2) { |
| 117 | + if (s1.length() != s2.length()) return false; |
| 118 | + |
| 119 | + int[] counts = new int[26]; |
| 120 | + for (int i = 0; i < s1.length(); i++) { |
| 121 | + counts[s1.charAt(i) - 'a']++; |
| 122 | + counts[s2.charAt(i) - 'a']--; |
| 123 | + } |
| 124 | + |
| 125 | + for (int count : counts) { |
| 126 | + if (count != 0) return false; |
| 127 | + } |
| 128 | + |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Code (Python) |
| 135 | + |
| 136 | +```python |
| 137 | +class Solution: |
| 138 | + def areAnagrams(self, s1, s2): |
| 139 | + if len(s1) != len(s2): |
| 140 | + return False |
| 141 | + |
| 142 | + counts = [0] * 26 |
| 143 | + for i in range(len(s1)): |
| 144 | + counts[ord(s1[i]) - ord('a')] += 1 |
| 145 | + counts[ord(s2[i]) - ord('a')] -= 1 |
| 146 | + |
| 147 | + for count in counts: |
| 148 | + if count != 0: |
| 149 | + return False |
| 150 | + |
| 151 | + return True |
| 152 | +``` |
| 153 | + |
| 154 | +## Contribution and Support |
| 155 | + |
| 156 | +For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: [Any Questions](https://www.linkedin.com/in/het-patel-8b110525a/). Let’s make this learning journey more collaborative! |
| 157 | + |
| 158 | +⭐ If you find this helpful, please give this repository a star! ⭐ |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +<div align="center"> |
| 163 | + <h3><b>📍Visitor Count</b></h3> |
| 164 | +</div> |
| 165 | + |
| 166 | +<p align="center"> |
| 167 | + <img src="https://profile-counter.glitch.me/Hunterdii/count.svg" /> |
| 168 | +</p> |
0 commit comments