From 9aee3ecf07baa364fe348d7d36c1afaccf0220f6 Mon Sep 17 00:00:00 2001 From: thutasann Date: Sat, 22 Feb 2025 17:15:28 +0800 Subject: [PATCH] add: hm examples --- .../HashMap/java/IsomorphicStrings.java | 39 ++++++++++++++ .../java/LongestConsecutiveSequence.java | 40 +++++++++++++++ .../Hashes/HashMap/java/WordPattern.java | 51 +++++++++++++++++++ .../longest_consecutive_sequence.ts | 25 +++++++++ .../Hashes/HashMap/typescript/word_pattern.ts | 26 ++++++++++ 5 files changed, 181 insertions(+) create mode 100644 data_structures/src/KeyPatterns/Hashes/HashMap/java/IsomorphicStrings.java create mode 100644 data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestConsecutiveSequence.java create mode 100644 data_structures/src/KeyPatterns/Hashes/HashMap/java/WordPattern.java create mode 100644 data_structures/src/KeyPatterns/Hashes/HashMap/typescript/longest_consecutive_sequence.ts create mode 100644 data_structures/src/KeyPatterns/Hashes/HashMap/typescript/word_pattern.ts diff --git a/data_structures/src/KeyPatterns/Hashes/HashMap/java/IsomorphicStrings.java b/data_structures/src/KeyPatterns/Hashes/HashMap/java/IsomorphicStrings.java new file mode 100644 index 0000000..49b7e55 --- /dev/null +++ b/data_structures/src/KeyPatterns/Hashes/HashMap/java/IsomorphicStrings.java @@ -0,0 +1,39 @@ +package KeyPatterns.Hashes.HashMap.java; + +import java.util.HashMap; +import java.util.Map; + +/** + * Isomorphic Strings + * - Two Strings `s` and `t` are isomorphic + * - if characters in `s` can be replaced to get `t` + */ +public class IsomorphicStrings { + public static void main(String[] args) { + System.out.println(isIsomorphic("egg", "add")); + } + + private static boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) { + return false; + } + + Map sToT = new HashMap<>(); + Map tTos = new HashMap<>(); + + for (int i = 0; i < s.length(); i++) { + char sc = s.charAt(i); + char tc = t.charAt(i); + + if (sToT.containsKey(sc) && sToT.get(sc) != tc) + return false; + if (tTos.containsKey(tc) && tTos.get(tc) != sc) + return false; + + sToT.put(sc, tc); + tTos.put(tc, sc); + } + + return true; + } +} diff --git a/data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestConsecutiveSequence.java b/data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestConsecutiveSequence.java new file mode 100644 index 0000000..d5bfb81 --- /dev/null +++ b/data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestConsecutiveSequence.java @@ -0,0 +1,40 @@ +package KeyPatterns.Hashes.HashMap.java; + +import java.util.HashSet; +import java.util.Set; + +/** + * Longest Consecutive Sequence + * - Given an unsorted array of integers `nums`, + * - Return the length of the longest consecutive sequence. + */ +public class LongestConsecutiveSequence { + public static void main(String[] args) { + int[] nums = { 100, 4, 200, 1, 3, 2 }; + System.out.println(longestConsecutive(nums)); + } + + private static int longestConsecutive(int[] nums) { + Set numSet = new HashSet<>(); + for (int num : nums) { + numSet.add(num); + } + + int longest = 0; + for (int num : nums) { + if (!numSet.contains(num - 1)) { + int currentNum = num; + int currentStreak = 1; + + while (numSet.contains(currentNum + 1)) { + currentNum++; + currentStreak++; + } + + longest = Math.max(longest, currentStreak); + } + } + + return longest; + } +} diff --git a/data_structures/src/KeyPatterns/Hashes/HashMap/java/WordPattern.java b/data_structures/src/KeyPatterns/Hashes/HashMap/java/WordPattern.java new file mode 100644 index 0000000..7d70cb7 --- /dev/null +++ b/data_structures/src/KeyPatterns/Hashes/HashMap/java/WordPattern.java @@ -0,0 +1,51 @@ +package KeyPatterns.Hashes.HashMap.java; + +import java.util.HashMap; +import java.util.Map; + +/** + * Word Pattern + * - Given a pattern string `pattern` and a string `s`, determine if `s` follows + * the same pattern. + * - Each letter in `pattern` maps to exactly one unique word in `s`. + * - Each unique word in `s` maps to exactly one letter in `pattern`. + * + * ## Example + * - Input: pattern = "abba", s = "dog cat cat dog" + * - 'a' -> "dog" + * - 'b' -> "cat" + * - 'b' -> "cat" + * - 'a' -> "dog" // consistent + */ +public class WordPattern { + public static void main(String[] args) { + System.out.println(wordPattern("abba", "dog cat cat dog")); + } + + private static boolean wordPattern(String pattern, String s) { + String[] words = s.split(" "); + if (pattern.length() != words.length) { + return false; + } + + Map charToWord = new HashMap<>(); + Map wordToChar = new HashMap<>(); + + for (int i = 0; i < pattern.length(); i++) { + char ch = pattern.charAt(i); + String word = words[i]; + + if (charToWord.containsKey(ch) && !charToWord.get(ch).equals(word)) { + return false; + } + if (wordToChar.containsKey(word) && wordToChar.get(word) != ch) { + return false; + } + + charToWord.put(ch, word); + wordToChar.put(word, ch); + } + + return true; + } +} diff --git a/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/longest_consecutive_sequence.ts b/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/longest_consecutive_sequence.ts new file mode 100644 index 0000000..0d5f7d5 --- /dev/null +++ b/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/longest_consecutive_sequence.ts @@ -0,0 +1,25 @@ +function longestConsecutiveSequence(nums: number[]) { + const numSet = new Set(); + for (const num of nums) { + numSet.add(num); + } + + let longest = 0; + for (const num of nums) { + if (!numSet.has(num - 1)) { + let currentNum = num; + let currentStreak = 1; + + while (numSet.has(currentNum + 1)) { + currentNum++; + currentStreak++; + } + + longest = Math.max(longest, currentStreak); + } + } + + return longest; +} + +console.log(longestConsecutiveSequence([100, 4, 200, 1, 3, 2])); diff --git a/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/word_pattern.ts b/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/word_pattern.ts new file mode 100644 index 0000000..e062656 --- /dev/null +++ b/data_structures/src/KeyPatterns/Hashes/HashMap/typescript/word_pattern.ts @@ -0,0 +1,26 @@ +function wordPattern(pattern: string, s: string) { + const words = s.split(' '); + if (pattern.length !== words.length) return false; + + const charToWord = new Map(); + const wordToChar = new Map(); + + for (let i = 0; i < pattern.length; i++) { + const ch = pattern.charAt(i); + const word = words[i]; + + if (charToWord.has(ch) && charToWord.get(ch) !== word) { + return false; + } + + if (wordToChar.has(word) && wordToChar.get(word) !== ch) { + return false; + } + + charToWord.set(ch, word); + wordToChar.set(word, ch); + } + + return true; +} +console.log(wordPattern('abba', 'dog cat cat dog'));