bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
string rest = t;
for (char ch : s) {
size_t at = rest.find(ch);
if (at == string::npos) return false;
rest.erase(at, 1);
}
return rest.empty();
}
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
StringBuilder rest = new StringBuilder(t);
for (char ch : s.toCharArray()) {
int at = rest.indexOf(String.valueOf(ch));
if (at == -1) return false;
rest.deleteCharAt(at);
}
return rest.length() == 0;
}
def is_anagram(s: str, t: str) -> bool:
if len(s) != len(t): return False
rest = list(t)
for ch in s:
at = rest.index(ch) if ch in rest else -1
if at == -1: return False
rest.pop(at)
return len(rest) == 0
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;
const rest = t.split('');
for (const ch of s) {
const at = rest.indexOf(ch);
if (at === -1) return false;
rest.splice(at, 1);
}
return rest.length === 0;
}
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
string a = s; sort(a.begin(), a.end());
string b = t; sort(b.begin(), b.end());
for (size_t i = 0; i < a.size(); i++) {
if (a[i] != b[i]) return false;
}
return true;
}
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
char[] a = s.toCharArray(); Arrays.sort(a);
char[] b = t.toCharArray(); Arrays.sort(b);
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
def is_anagram(s: str, t: str) -> bool:
if len(s) != len(t): return False
a = sorted(s)
b = sorted(t)
for i in range(len(a)):
if a[i] != b[i]: return False
return True
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;
const a = [...s].sort();
const b = [...t].sort();
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
unordered_map<char, int> count;
for (char ch : s) count[ch]++;
for (char ch : t) {
int left = count[ch];
if (left == 0) return false;
count[ch] = left - 1;
}
return true;
}
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
Map<Character, Integer> count = new HashMap<>();
for (char ch : s.toCharArray()) count.merge(ch, 1, Integer::sum);
for (char ch : t.toCharArray()) {
int left = count.getOrDefault(ch, 0);
if (left == 0) return false;
count.put(ch, left - 1);
}
return true;
}
def is_anagram(s: str, t: str) -> bool:
if len(s) != len(t): return False
count: dict[str, int] = {}
for ch in s: count[ch] = count.get(ch, 0) + 1
for ch in t:
left = count.get(ch, 0)
if left == 0: return False
count[ch] = left - 1
return True
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;
const count = new Map<string, number>();
for (const ch of s) count.set(ch, (count.get(ch) ?? 0) + 1);
for (const ch of t) {
const left = count.get(ch) ?? 0;
if (left === 0) return false;
count.set(ch, left - 1);
}
return true;
}
bool isAnagram(const string& s, const string& t) {
// static types make a runtime type guard unnecessary
if (s.size() != t.size()) return false;
int counts[256] = {0}; // one slot per byte value
int nonZero = 0;
for (size_t i = 0; i < s.size(); i++) {
unsigned char a = s[i];
unsigned char b = t[i];
if (counts[a]++ == 0) nonZero++;
else if (counts[a] == 0) nonZero--;
if (counts[b]-- == 0) nonZero++;
else if (counts[b] == 0) nonZero--;
}
return nonZero == 0;
}
public boolean isAnagram(String s, String t) {
if (s == null || t == null) return false;
if (s.length() != t.length()) return false;
int[] counts = new int[65536]; // one slot per UTF-16 code unit
int nonZero = 0;
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (counts[a]++ == 0) nonZero++;
else if (counts[a] == 0) nonZero--;
if (counts[b]-- == 0) nonZero++;
else if (counts[b] == 0) nonZero--;
}
return nonZero == 0;
}
def is_anagram(s: str, t: str) -> bool:
if not isinstance(s, str) or not isinstance(t, str): return False
if len(s) != len(t): return False
counts: dict[str, int] = {} # sparse table keyed by character
non_zero = 0
for i in range(len(s)):
a, b = s[i], t[i]
old = counts.get(a, 0); counts[a] = old + 1
non_zero += (old + 1 != 0) - (old != 0)
old = counts.get(b, 0); counts[b] = old - 1
non_zero += (old - 1 != 0) - (old != 0)
return non_zero == 0
function isAnagram(s: string, t: string): boolean {
if (typeof s !== 'string' || typeof t !== 'string') return false;
if (s.length !== t.length) return false;
const counts = new Int32Array(65536);
let nonZero = 0;
for (let i = 0; i < s.length; i++) {
const a = s.charCodeAt(i);
const b = t.charCodeAt(i);
if (counts[a]++ === 0) nonZero++;
else if (counts[a] === 0) nonZero--;
if (counts[b]-- === 0) nonZero++;
else if (counts[b] === 0) nonZero--;
}
return nonZero === 0;
}