-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
data_structures/src/KeyPatterns/Hashes/HashMap/java/IsomorphicStrings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestConsecutiveSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
data_structures/src/KeyPatterns/Hashes/HashMap/java/WordPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
data_structures/src/KeyPatterns/Hashes/HashMap/typescript/longest_consecutive_sequence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); |
26 changes: 26 additions & 0 deletions
26
data_structures/src/KeyPatterns/Hashes/HashMap/typescript/word_pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |