From 8d631a62960298b1e5f65b89b5e6f48df9e3bbaa Mon Sep 17 00:00:00 2001 From: Nikesh Manjunath Date: Sat, 3 Jan 2026 23:10:13 -0800 Subject: [PATCH] Completed Hashing 1 --- Leetcode205.java | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ Leetcode290.java | 33 ++++++++++++++++++++++++++++++ Leetcode49.java | 23 +++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 Leetcode205.java create mode 100644 Leetcode290.java create mode 100644 Leetcode49.java diff --git a/Leetcode205.java b/Leetcode205.java new file mode 100644 index 00000000..56ebb3a3 --- /dev/null +++ b/Leetcode205.java @@ -0,0 +1,53 @@ + +// TC - O(n) +// SC - O(1) Since each dataset we created can have only 26 characters. +class Solution { + public boolean isIsomorphic(String s, String t) { + // Solution 1: Two HashMaps + // HashMap sMap = new HashMap<>(); + // HashMap tMap = new HashMap<>(); + // for (int i =0; i< s.length(); i++) { + // char sChar = s.charAt(i); + // char tChar = t.charAt(i); + // if(sMap.containsKey(sChar)) { + // if(sMap.get(sChar) != tChar) { + // return false; + // } + // } else { + // sMap.put(sChar, tChar); + // } + + // if(tMap.containsKey(tChar)) { + // if(tMap.get(tChar) != sChar) { + // return false; + // } + // } else { + // tMap.put(tChar, sChar); + // } + + // } + + // return true; + + // Solution 2: One HashMap and Hashset. + HashMap sMap = new HashMap<>(); + HashSet sSet = new HashSet<>(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!sMap.containsKey(c)) { // Character not present in the map + if (sSet.contains(t.charAt(i))) { // Character in t already mapped to some other character in s + return false; + } + sMap.put(c, t.charAt(i)); // adding {s Character: t Character} key value pair + sSet.add(t.charAt(i)); // adding t Character to the Hashset to indicate that it is already mapped to an + // s character. + } else { + if (sMap.get(c) != t.charAt(i)) { // character present but mapped to some other character + return false; + } + } + } + return true; + + } +} \ No newline at end of file diff --git a/Leetcode290.java b/Leetcode290.java new file mode 100644 index 00000000..408e18dd --- /dev/null +++ b/Leetcode290.java @@ -0,0 +1,33 @@ +TC: O(n) +SC: O(1) Since max of 26 characters can be there in the pattern and each word can be mapped to only one character. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class Solution { + public boolean wordPattern(String pattern, String s) { + String[] words = s.split("\\s"); + if (words.length != pattern.length()) + return false; // check if each word can be associated with a character or not. If not, return + // false since there's no 1:1 mapping possible. + + HashMap charMap = new HashMap<>(); + HashMap stringMap = new HashMap<>(); + int k = 0; + for (int i = 0; i < pattern.length(); i++) { + char c = pattern.charAt(i); + if (charMap.get(c) == null) { // Character not present in the charMap + if (stringMap.get(words[i]) == null) { // String not present in the stringMap + charMap.put(c, words[i]); // Add {char: string} key value pair + stringMap.put(words[i], c);// Add {string: char} key value pair + } else { // String already mapped to some other character + return false; + } + + } else { + if (!charMap.get(c).equals(words[i])) { // Character present but mapped to some other string + return false; + } + } + } + return true; + } +} \ No newline at end of file diff --git a/Leetcode49.java b/Leetcode49.java new file mode 100644 index 00000000..ded9fffb --- /dev/null +++ b/Leetcode49.java @@ -0,0 +1,23 @@ +// TC : O(n) +// SC : O(n) +class Solution { + public List> groupAnagrams(String[] strs) { + // Solution 1: Checking the anagrams by sorting. + HashMap> map = new HashMap<>(); + for (int i = 0; i < strs.length; i++) { + String curr = strs[i]; + // sort + char[] charArr = curr.toCharArray(); + Arrays.sort(charArr); + String sorted = String.valueOf(charArr); + + if (!map.containsKey(sorted)) { + map.put(sorted, new ArrayList<>()); + } + List li = map.get(sorted); + li.add(curr); + } + return new ArrayList<>(map.values()); + + } +} \ No newline at end of file