|
| 1 | +--- |
| 2 | +id: valid-anagram |
| 3 | +title: Valid Anagram |
| 4 | +sidebar_label: 0242 Valid Anagram |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Hash Table |
| 8 | + - Sorting |
| 9 | + - LeetCode |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - C++ |
| 13 | +description: "This is a solution to the Valid Anagram problem on LeetCode." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given two strings `s` and `t`, return true if `t` is an anagram of `s`, and false otherwise. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: s = "anagram", t = "nagaram" |
| 28 | +Output: true |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: s = "rat", t = "car" |
| 35 | +Output: false |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- $1 <= s.length, t.length <= 5 * 10^4$ |
| 41 | +- s and t consist of lowercase English letters. |
| 42 | + |
| 43 | +## Solution for Valid Anagram Problem |
| 44 | + |
| 45 | +### Approach |
| 46 | + |
| 47 | +To determine if two strings `s` and `t` are anagrams, we can use the following approach: |
| 48 | + |
| 49 | +1. **Character Counting**: Count the occurrences of each character in both strings using arrays of size 26 (since there are 26 lowercase letters). |
| 50 | +2. **Comparison**: Compare the two arrays. If they are identical, then `t` is an anagram of `s`. |
| 51 | + |
| 52 | +### Code in Different Languages |
| 53 | + |
| 54 | +<Tabs> |
| 55 | + <TabItem value="Python" label="Python" default> |
| 56 | + <SolutionAuthor name="@mahek0620"/> |
| 57 | + ```python |
| 58 | + |
| 59 | + class Solution: |
| 60 | + def isAnagram(self, s: str, t: str) -> bool: |
| 61 | + if len(s) != len(t): |
| 62 | + return False |
| 63 | + |
| 64 | + # Initialize arrays to count occurrences of each character |
| 65 | + count_s = [0] * 26 |
| 66 | + count_t = [0] * 26 |
| 67 | + |
| 68 | + # Count occurrences of each character in s |
| 69 | + for char in s: |
| 70 | + count_s[ord(char) - ord('a')] += 1 |
| 71 | + |
| 72 | + # Count occurrences of each character in t |
| 73 | + for char in t: |
| 74 | + count_t[ord(char) - ord('a')] += 1 |
| 75 | + |
| 76 | + # Compare the two arrays |
| 77 | + return count_s == count_t |
| 78 | + ``` |
| 79 | + </TabItem> |
| 80 | + <TabItem value="Java" label="Java"> |
| 81 | + <SolutionAuthor name="@mahek0620"/> |
| 82 | + ```java |
| 83 | + |
| 84 | + class Solution { |
| 85 | + public boolean isAnagram(String s, String t) { |
| 86 | + if (s.length() != t.length()) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // Initialize arrays to count occurrences of each character |
| 91 | + int[] count_s = new int[26]; |
| 92 | + int[] count_t = new int[26]; |
| 93 | + |
| 94 | + // Count occurrences of each character in s |
| 95 | + for (char ch : s.toCharArray()) { |
| 96 | + count_s[ch - 'a']++; |
| 97 | + } |
| 98 | + |
| 99 | + // Count occurrences of each character in t |
| 100 | + for (char ch : t.toCharArray()) { |
| 101 | + count_t[ch - 'a']++; |
| 102 | + } |
| 103 | + |
| 104 | + // Compare the two arrays |
| 105 | + return Arrays.equals(count_s, count_t); |
| 106 | + } |
| 107 | + } |
| 108 | + ``` |
| 109 | + </TabItem> |
| 110 | + <TabItem value="C++" label="C++"> |
| 111 | + <SolutionAuthor name="@mahek0620"/> |
| 112 | + ```cpp |
| 113 | + |
| 114 | + #include <vector> |
| 115 | + using namespace std; |
| 116 | + |
| 117 | + class Solution { |
| 118 | + public: |
| 119 | + bool isAnagram(string s, string t) { |
| 120 | + if (s.length() != t.length()) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + // Initialize arrays to count occurrences of each character |
| 125 | + vector<int> count_s(26, 0); |
| 126 | + vector<int> count_t(26, 0); |
| 127 | + |
| 128 | + // Count occurrences of each character in s |
| 129 | + for (char ch : s) { |
| 130 | + count_s[ch - 'a']++; |
| 131 | + } |
| 132 | + |
| 133 | + // Count occurrences of each character in t |
| 134 | + for (char ch : t) { |
| 135 | + count_t[ch - 'a']++; |
| 136 | + } |
| 137 | + |
| 138 | + // Compare the two arrays |
| 139 | + return count_s == count_t; |
| 140 | + } |
| 141 | + }; |
| 142 | + ``` |
| 143 | + |
| 144 | + </TabItem> |
| 145 | +</Tabs> |
| 146 | + |
| 147 | +### Complexity Analysis |
| 148 | + |
| 149 | +- **Time complexity**: O(n), where n is the length of the strings `s` and `t`. We iterate through both strings once to count characters and compare counts, which are both O(n). |
| 150 | +- **Space complexity**: O(1) for the fixed-size arrays in Python and O(26) = O(1) for arrays in Java and C++, since there are only 26 lowercase letters. |
| 151 | + |
| 152 | +## References |
| 153 | + |
| 154 | +- **LeetCode Problem:** [Valid Anagram](https://leetcode.com/problems/valid-anagram/) |
| 155 | +- **Solution Link:** [Valid Anagram Solution on LeetCode](https://leetcode.com/problems/valid-anagram/solution/) |
| 156 | +- **Author's GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments