From 1133a623a2890a5ea47802d4aeb2d287002eda0f Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Sat, 2 Nov 2024 22:16:25 -0400 Subject: [PATCH] problems --- .idea/.gitignore | 8 ++++++ .idea/Hashing-1.iml | 11 ++++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 ++++ problem1.java | 47 ++++++++++++++++++++++++++++++ problem2.java | 61 +++++++++++++++++++++++++++++++++++++++ problem3.java | 69 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 216 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Hashing-1.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 problem1.java create mode 100644 problem2.java create mode 100644 problem3.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Hashing-1.iml b/.idea/Hashing-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Hashing-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..07115cdf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..c12cdf14 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..0d217627 --- /dev/null +++ b/problem1.java @@ -0,0 +1,47 @@ +//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. + +//Time complexity: O(n*k) +//Space complexity: O(n*k) + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class problem1 { + + public List> groupAnagrams(String[] strs) + { + HashMap> map= new HashMap<>(); + + for (String word: strs) + { + double hash= getHash(word); + if (!map.containsKey(hash)) + { + map.put(hash, new ArrayList<>()); + } + map.get(hash).add(word); + } + return new ArrayList<>(map.values()); + } + private double getHash(String word) + { + double hash=1; + int[] prime= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,61,67,71,73,79,83,89,97,100}; + for (char c: word.toCharArray()) + { + hash = hash * prime[c - 'a']; + } + return hash; + } + + public static void main(String[] args) + { + + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..7212108d --- /dev/null +++ b/problem2.java @@ -0,0 +1,61 @@ +//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. + +//Time complexity: O(n) +//Space complexity: O(n) + + +import java.util.HashMap; +import java.util.HashSet; + +public class problem2 { + private static boolean isIsomorphic(String a, String b) + { + if (a==null && b==null) + { + return true; + } + if(a==null || b==null) + { + return false; + } + if (a.length() != b.length()) + { + return false; + } + HashMap sMap= new HashMap<>(); + HashSet sSet= new HashSet<>(); + for (int i=0;i map=new HashMap<>(); + String[] str=s.split(" "); + HashSet set= new HashSet<>(); + for (int i=0;i