diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..5cd3857e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1768803248350 + + + + + + + \ No newline at end of file diff --git a/GroupAnagrams.java b/GroupAnagrams.java new file mode 100644 index 00000000..ba238d82 --- /dev/null +++ b/GroupAnagrams.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +public class GroupAnagrams { + public List> groupAnagrams(String[] strs){ + HashMap> map = new HashMap<>(); + for(int i=0;i()); //map = {"aet" : []} creates new empty list + } + //get list of all Strings + List li = map.get(sorted); + li.add(curr); //add to list + map.put(sorted,li);//add to map + } + return new ArrayList<>(map.values()); + } +} + +//TC: O(N KlogK) +//SC: O(NK) \ No newline at end of file diff --git a/IsIsomorphic.java b/IsIsomorphic.java new file mode 100644 index 00000000..c172baa6 --- /dev/null +++ b/IsIsomorphic.java @@ -0,0 +1,42 @@ +import java.util.HashMap; + +public class IsIsomorphic { + public static void main(String args[]){ + String s = "paper"; + String t = "title"; + System.out.println(isIsomorphic(s,t)); + } + public static boolean isIsomorphic(String s, String t) { + int sl = s.length(); + int tl = t.length(); + //create two HashMaps to store characters of s & t + HashMap sMap = new HashMap<>(); + HashMap tMap = new HashMap<>(); + for(int i=0; i < sl; i++){ + char sChar = s.charAt(i); + char tChar = t.charAt(i); + //s to t mapping + if(sMap.containsKey(sChar)){ + //if earlier mapped character of s is equal to t, if not return false --> breech + if(sMap.get(sChar) != tChar){ + return false; + } + } else{ + sMap.put(sChar, tChar); + } + // t to s mapping + if(tMap.containsKey(tChar)){ + //if earlier mapped character of t is equal to s, if not return false --> breech + if(tMap.get(tChar) != sChar){ + return false; + } + } else{ + tMap.put(tChar, sChar); + } + } + return true; + } +} + +// TC : O(N) +//SC : O(1) \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/.gitignore b/out/production/Hashing-1/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Hashing-1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Hashing-1/.idea/Hashing-1.iml b/out/production/Hashing-1/.idea/Hashing-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Hashing-1/.idea/Hashing-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/codeStyles/Project.xml b/out/production/Hashing-1/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Hashing-1/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/codeStyles/codeStyleConfig.xml b/out/production/Hashing-1/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Hashing-1/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/misc.xml b/out/production/Hashing-1/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Hashing-1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/modules.xml b/out/production/Hashing-1/.idea/modules.xml new file mode 100644 index 00000000..c12cdf14 --- /dev/null +++ b/out/production/Hashing-1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/uiDesigner.xml b/out/production/Hashing-1/.idea/uiDesigner.xml new file mode 100644 index 00000000..2b63946d --- /dev/null +++ b/out/production/Hashing-1/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/.idea/vcs.xml b/out/production/Hashing-1/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Hashing-1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Hashing-1/IsIsomorphic.class b/out/production/Hashing-1/IsIsomorphic.class new file mode 100644 index 00000000..e2baf9a7 Binary files /dev/null and b/out/production/Hashing-1/IsIsomorphic.class differ diff --git a/out/production/Hashing-1/README.md b/out/production/Hashing-1/README.md new file mode 100644 index 00000000..17be34ff --- /dev/null +++ b/out/production/Hashing-1/README.md @@ -0,0 +1,60 @@ +# Hashing-1 +Explain your approach in **three sentences only** at top of your code + + +## Problem 1: +Given an array of strings, group anagrams together. + +Example: +Input: ["eat", "tea", "tan", "ate", "nat", "bat"], +Output: +[ + ["ate","eat","tea"], + ["nat","tan"], + ["bat"] +] + +Note: +All inputs will be in lowercase. +The order of your output does not matter. + +## Problem 2: +Given two strings s and t, determine if they are isomorphic. +Two strings are isomorphic if the characters in s can be replaced to get t. +All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. + +Example 1: +Input: s = "egg", t = "add" +Output: true + +Example 2: +Input: s = "foo", t = "bar" +Output: false + +Example 3: +Input: s = "paper", t = "title" +Output: true +Note: +You may assume both s and t have the same length. + +## Problem 3: +Given a pattern and a string str, find if str follows the same pattern. +Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. + +Example 1: +Input: pattern = "abba", str = "dog cat cat dog" +Output: true + +Example 2: +Input:pattern = "abba", str = "dog cat cat fish" +Output: false + +Example 3: +Input: pattern = "aaaa", str = "dog cat cat dog" +Output: false + +Example 4: +Input: pattern = "abba", str = "dog dog dog dog" +Output: false +Notes: +You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.