titan2400

Valid Anagrams - LeetCode

Oct 27th, 2025
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | Source Code | 0 0
  1. // Valid Anagrams - https://leetcode.com/problems/valid-anagram/sou
  2.  
  3. class Solution {
  4.  
  5.     // Sort the underlying character arrays & compare
  6.     // For anagrams they should be an exact match
  7.     // Time Complexity: O(nlogn)
  8.     // Space Complexity: O(n)
  9.     // public boolean isAnagram(String s, String t) {
  10.     //     if(s == null && t == null) {
  11.     //         return true;
  12.     //     }
  13.  
  14.     //     if(s == null || t == null) {
  15.     //         return false;
  16.     //     }
  17.  
  18.     //     if(s.length() != t.length()) {
  19.     //         return false;
  20.     //     }
  21.  
  22.  
  23.     //     char[] sChars = s.toCharArray();
  24.     //     char[] tChars = t.toCharArray();
  25.  
  26.     //     Arrays.sort(sChars);
  27.     //     Arrays.sort(tChars);
  28.  
  29.     //     for(int i = 0; i < s.length(); i++) {
  30.     //         if(sChars[i] != tChars[i]) {
  31.     //             return false;
  32.     //         }
  33.     //     }
  34.  
  35.     //     return true;
  36.     // }
  37.  
  38.     // Using HashMap to count characters in one of the strings
  39.     // and using the other string to decrement counts in Hashmap
  40.     // Time Complexity: O(n)
  41.     // Space Complexity: O(k)
  42.     //   where k is number of unique characters in the string
  43.     // LeetCode indicate above solution to be much better despite the complexity
  44.     // public boolean isAnagram(String s, String t) {
  45.     //     if(s == null && t == null) {
  46.     //         return true;
  47.     //     }
  48.  
  49.     //     if(s == null || t == null) {
  50.     //         return false;
  51.     //     }
  52.  
  53.     //     if(s.length() != t.length()) {
  54.     //         return false;
  55.     //     }
  56.  
  57.     //     Map<Character, Integer> count = new HashMap<>();
  58.  
  59.     //     for (int i = 0; i < s.length(); i++) {
  60.     //         count.put(s.charAt(i), count.getOrDefault(s.charAt(i), 0) + 1);
  61.     //     }
  62.  
  63.     //     for (int i = 0; i < t.length(); i++) {
  64.     //         char c = t.charAt(i);
  65.     //         if(!count.containsKey(c)) {
  66.     //             return false;
  67.     //         }
  68.  
  69.     //         int counter = count.get(c) - 1;
  70.     //         if (counter < 0) {
  71.     //             return false;
  72.     //         }
  73.  
  74.     //         count.put(c, counter);
  75.     //     }
  76.  
  77.     //     for(Integer countValue: count.values()) {
  78.     //         if (countValue != 0) {
  79.     //             return false;
  80.     //         }
  81.     //     }
  82.  
  83.     //     return true;
  84.     // }
  85.  
  86.     // Using Array to count characters
  87.     // Time Complexity: O(n)
  88.     // Space Complexity: O(1)
  89.     // LeetCode indicate sorting solution to be much better despite the complexity
  90.     public boolean isAnagram(String s, String t) {
  91.         if(s == null && t == null) {
  92.             return true;
  93.         }
  94.  
  95.         if(s == null || t == null) {
  96.             return false;
  97.         }
  98.  
  99.         if(s.length() != t.length()) {
  100.             return false;
  101.         }
  102.  
  103.         int[] freq = new int[26];
  104.         for (int i = 0; i < s.length(); i++) {
  105.             freq[s.charAt(i) - 'a']++;
  106.             freq[t.charAt(i) - 'a']--;
  107.         }
  108.  
  109.         for(int i = 0; i < freq.length; i++) {
  110.             if(freq[i] != 0) {
  111.                 return false;
  112.             }
  113.         }
  114.  
  115.         return true;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment