From 7788f2b5b876a7f981ef32f0ab276599d4722e3e Mon Sep 17 00:00:00 2001 From: sainathek Date: Mon, 12 Jan 2026 02:11:45 -0500 Subject: [PATCH] Done Hasing-1 --- problem1.java | 33 +++++++++++++++++++++++++++++++++ problem2.java | 34 ++++++++++++++++++++++++++++++++++ problem3.java | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java create mode 100644 problem3.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..20bf8674 --- /dev/null +++ b/problem1.java @@ -0,0 +1,33 @@ +// Time Complexity : O(n * k log k) (n = number of words, k = avg length of word) +// Space Complexity : O(n * k) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just needed a common key to group same anagrams. + + +// Your code here along with comments explaining your approach in three sentences only +// For every word, we sort its characters and use that sorted string as a key. +// All words having same sorted key are anagrams, so we store them together in a map. +// Finally, we return all grouped lists from the map. + +import java.util.*; + +class Solution { + public List> groupAnagrams(String[] strs) { + + HashMap> map = new HashMap<>(); + + for (String s : strs) { + + char[] arr = s.toCharArray(); + Arrays.sort(arr); + String key = new String(arr); + + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(s); + } + + return new ArrayList<>(map.values()); + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..56ffd32b --- /dev/null +++ b/problem2.java @@ -0,0 +1,34 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) (fixed size arrays of 256) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, only had to make sure mapping works both ways. + + +// Your code here along with comments explaining your approach in three sentences only +// We keep a mapping from characters in s to characters in t and also from t to s. +// While scanning, if an existing mapping does not match the current characters, return false. +// If we finish without conflicts, then both strings are isomorphic. + + +class Solution { + public boolean isIsomorphic(String s, String t) { + + int[] map1 = new int[256]; + int[] map2 = new int[256]; + + for (int i = 0; i < s.length(); i++) { + + char c1 = s.charAt(i); + char c2 = t.charAt(i); + + if (map1[c1] == 0 && map2[c2] == 0) { + map1[c1] = c2; + map2[c2] = c1; + } else { + if (map1[c1] != c2 || map2[c2] != c1) return false; + } + } + + return true; + } +} diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..9ad52a3c --- /dev/null +++ b/problem3.java @@ -0,0 +1,39 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just needed to ensure one-to-one mapping (bijection). + + +// Your code here along with comments explaining your approach in three sentences only +// We split the string into words and check if number of words matches pattern length. +// Then we map each pattern character to one word and also map each word back to one character. +// If at any point mapping conflicts, return false, otherwise return true. + +import java.util.*; + +class Solution { + public boolean wordPattern(String pattern, String s) { + + String[] words = s.split(" "); + if (pattern.length() != words.length) return false; + + HashMap map1 = new HashMap<>(); + HashMap map2 = new HashMap<>(); + + for (int i = 0; i < pattern.length(); i++) { + + char ch = pattern.charAt(i); + String w = words[i]; + + if (!map1.containsKey(ch) && !map2.containsKey(w)) { + map1.put(ch, w); + map2.put(w, ch); + } else { + if (!map1.containsKey(ch) || !map2.containsKey(w)) return false; + if (!map1.get(ch).equals(w) || map2.get(w) != ch) return false; + } + } + + return true; + } +}