Skip to content

Instantly share code, notes, and snippets.

@Merce0897
Created October 27, 2022 17:03
Show Gist options
  • Save Merce0897/752656dc9b56260f3948ec18653090b1 to your computer and use it in GitHub Desktop.
Save Merce0897/752656dc9b56260f3948ec18653090b1 to your computer and use it in GitHub Desktop.
// Time: O(n) -- Space: O(n) -- Two Sum
function twoSum(nums: number[], target: number): number[] {
// Create map
let output = new Map()
// Loop the array
for(let i = 0; i < nums.length; i++) {
// Create substract
let sub = target - nums[i]
// Check if map dont have sub will add to map
// If already in map, return index of sub and current
if (!output.has(sub)) {
output.set(nums[i],i)
} else {
return [output.get(sub),i]
}
}
return []
}
// Time: O(n) -- Space: O(n) -- Contains Duplicate
function containsDuplicate(nums: number[]): boolean {
// Create map
let result = new Map()
// Loop the array
for (let i = 0; i < nums.length; i++) {
// Check if map dont have so add element to map,
// Else will return array is duplicate
if (result.has(nums[i])) {
return true
} else {
result.set(nums[i],i)
}
}
return false
}
// Time: O(n) -- Space: O(n) -- Check if the sentence is Pangram
function checkIfPangram(sentence: string): boolean {
// Create map
let result = new Map()
// Loop the map
for (let i = 0; i < sentence.length; i++) {
// Add the element to map and check if size of map = amount of alphabet or not
if (!result.has(sentence.charAt(i))) {
result.set(sentence.charAt(i),i)
}
if (result.size === 26) {
return true
}
}
return false
};
// Time: O(n) -- Space: O(n) -- First letter to appear twice
function repeatedCharacter(s: string): string {
// Create map
let result = new Map()
// Loop the array
for (let i = 0; i < s.length;i++) {
// If map dont have so map will add element,
// If have element will be return
if (result.has(s.charAt(i))) {
return s.charAt(i)
} else {
result.set(s.charAt(i),i)
}
}
return ''
};
// Time: O(n) -- Space: O(n) -- Check if all characters have equal number of occurrences
function areOccurrencesEqual(s: string): boolean {
// Create map
let result = new Map()
// Loop the array
for (let i = 0; i < s.length; i++) {
// Create times of element appear and push element with its times to map
if (result.has(s.charAt(i))) {
let num = result.get(s.charAt(i))
num++
result.set(s.charAt(i),num)
} else {
result.set(s.charAt(i),1)
}
}
// Create a check variable
let occ = result.get(s.charAt(0))
// Check if all the element appear with the same times
for (const x of result.values()) {
if (x !== occ) {
return false
}
}
return true
};
// Time: O(n) -- Space: O(n) -- Valid Anagram
function isAnagram(s: string, t: string): boolean {
// Create map
let result = new Map()
// Loop the first string
for (let i = 0; i < s.length; i++) {
// Push all element with its appear times to map
if (result.has(s.charAt(i))) {
let num = result.get(s.charAt(i))
num++
result.set(s.charAt(i),num)
} else {
result.set(s.charAt(i),1)
}
}
// Loop the second string
for (let i = 0; i < t.length; i++) {
// Check the map if second string have element its value will sub 1
// If map dont have will create new element to map
if (result.has(t.charAt(i))) {
let num = result.get(t.charAt(i))
num--
result.set(t.charAt(i),num)
} else {
result.set(t.charAt(i),1)
}
}
// Check if map have any value diff 0 so not anagram
for (const x of result.values()) {
if (x !== 0) {
return false
}
}
return true
};
// Time: O(n) -- Space: O(n) -- Replace elements with greatest element on right side
function replaceElements(arr: number[]): number[] {
// Create check array
let result: number[] = [];
// Assign last element -1 for compare in loop
result[arr.length-1] = -1;
// Loop the array
for (let i = arr.length-2; i >= 0; i--) {
// The value of check array and input array with same index to get greatest element
result[i] = Math.max(result[i+1],arr[i+1]);
}
return result
};
// Time: O(n) -- Space: O(1) -- Remove Element
function removeElement(nums: number [], val: number): number {
// Create pointers, k is count variable
let k = 0
let i = 0
let j = 0
// Loop the array
while (i < nums.length) {
// If element diff val, k will increase
if (nums[i] !== val) {
k++
}
// One pointer to loop the array
// One pointer to get the val index and swap
if (nums[i] !== val && nums[j] === val) {
[nums[i], nums[j]] = [nums[j], nums[i]]
j++
}
if (nums[j] !== val) {
j++
}
i++
}
return k
};
// Time: O(n) -- Space: O(n) -- Isomorphic Strings
function isIsomorphic(s: string, t: string): boolean {
// Check if 2 input longer or shorter each other
if (s.length !== t.length) {
return false
}
// Create map and check array to check the value of map
let result = new Map()
let check: string[] = []
// Loop the array
for (let i = 0; i < s.length; i++) {
let Schar = s.charAt(i)
let Tchar = t.charAt(i)
// If map have element, check element in map if diff with array return false
if (result.has(Schar)) {
if (result.get(Schar) !== Tchar) {
return false
}
} else {
// Check the value of map if have element of second array
if (check.includes(Tchar)) { return false }
// Check if the element already have in map
if (Schar !== Tchar && Tchar === result.get(Tchar)) {
return false
} else {
// If element is not in map, map will set the element and push to check array
result.set(Schar,Tchar)
check.push(Tchar)
}
}
}
return true
};
@QuocCao-dev
Copy link

https://gist.github.com/Merce0897/752656dc9b56260f3948ec18653090b1#file-hash_table-ts-L13

 if (output.has(sub)) {
        return [output.get(sub),i]
        } 
   output.set(nums[i],i)
          
        

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment