From 8370fd2f3187a79c72ae35dc978f20105ac17b69 Mon Sep 17 00:00:00 2001 From: gurneetk186 Date: Tue, 24 Feb 2026 09:43:47 -0800 Subject: [PATCH] Done Hashing -1 --- Exercise1.java | 19 +++++++++++++++++++ Exercise2.java | 23 +++++++++++++++++++++++ Exercise3.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 Exercise1.java create mode 100644 Exercise2.java create mode 100644 Exercise3.java diff --git a/Exercise1.java b/Exercise1.java new file mode 100644 index 00000000..77b255e2 --- /dev/null +++ b/Exercise1.java @@ -0,0 +1,19 @@ +class Solution { + //I use a HahMap where the key is stored in sorted version of each string and the value is list of anagrams. + //Since anagrams share identical sorted character sequences, sorting normalizes them into the same key. + //I group all original strings under their sorted key and return all grouped values + public List> groupAnagrams(String[] strs) { + HashMap > map = new HashMap<>(); + for(String str : strs){ + char[] sortedArr = str.toCharArray(); + Arrays.sort(sortedArr); + String sortedStr = String.valueOf(sortedArr); + if(!map.containsKey(sortedStr)){ + map.put(sortedStr, new ArrayList<>()); + } + map.get(sortedStr).add(str); + } + return new ArrayList<>(map.values()); + } +} + diff --git a/Exercise2.java b/Exercise2.java new file mode 100644 index 00000000..57129736 --- /dev/null +++ b/Exercise2.java @@ -0,0 +1,23 @@ +class Solution { + // I use two hashmaps to maintain one-to-one mapping between characters s and t + //for each index, i chek whether an existing mapping conflicts in either direction,if it does, return false + //If I finish loop without conflicts, the string are isomorphic, return true + + public boolean isIsomorphic(String s, String t) { + HashMap STmap = new HashMap<>(); + HashMap TSmap = new HashMap<>(); + for(int i = 0; i < s.length(); i++){ + char c1 = s.charAt(i); + char c2 = t.charAt(i); + if(STmap.containsKey(c1) && STmap.get(c1) != c2) + return false; + if(TSmap.containsKey(c2) && TSmap.get(c2) != c1) + return false; + STmap.put(c1, c2); + TSmap.put(c2,c1); + + } + return true; + + } +} diff --git a/Exercise3.java b/Exercise3.java new file mode 100644 index 00000000..69bc1f15 --- /dev/null +++ b/Exercise3.java @@ -0,0 +1,29 @@ +class Solution { + // I split the string into words and checked if the pattern length matches the number of words. + // I used two hashmaps to maintain a bijetion - one mapaping pattern to words, other words to characters. + // while iterating, I return false if any existing mapping mismatchedl otherwise stored the mapping return true at the end. + + public boolean wordPattern(String pattern, String s) { + String[]words = s.split(" "); + if(pattern.length() != words.length){ + return false; + } + HashMap mapPT = new HashMap<>(); + HashMapmapTP = new HashMap<>(); + for (int i = 0; i < pattern.length(); i++){ + char ch = pattern.charAt(i); + String word = words[i]; + if(mapPT.containsKey(ch) && !mapPT.get(ch).equals(word)){ + return false; + + } + if(mapTP.containsKey(word)&&!mapTP.get(word).equals(ch)) { + return false; + } + mapPT.put(ch, word); + mapTP.put(word,ch); + } + return true; + + } +}