Skip to content

Commit

Permalink
add: hm examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Feb 22, 2025
1 parent 3c148c2 commit 9aee3ec
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Character, Character> sToT = new HashMap<>();
Map<Character, Character> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Character, String> charToWord = new HashMap<>();
Map<String, Character> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function longestConsecutiveSequence(nums: number[]) {
const numSet = new Set<number>();
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]));
Original file line number Diff line number Diff line change
@@ -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<string, string>();
const wordToChar = new Map<string, string>();

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'));

0 comments on commit 9aee3ec

Please sign in to comment.