Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add: typescript solutions for LC-1,LC-217,LC-242
  • Loading branch information
SandeepGamot committed Apr 13, 2022
commit 3cc31fa1d24996813e73fa28da4833e36c4e121f
11 changes: 11 additions & 0 deletions typescript/1-Two-Sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function twoSum(nums: number[], target: number): number[] {
const map = new Map();

for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) {
return [i, map.get(nums[i])];
} else {
map.set(target - nums[i], i);
}
}
}
10 changes: 10 additions & 0 deletions typescript/217-Contains-Duplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function containsDuplicate(nums: number[]): boolean {
const set = new Set();

for (let i = 0; i < nums.length; i++) {
if (set.has(nums[i])) return true;
else set.add(nums[i]);
}

return false;
}
16 changes: 16 additions & 0 deletions typescript/242-Valid-Anagrams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;

const store = new Array(26).fill(0);

for (let i = 0; i < s.length; i++) {
store[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
store[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
}

for (let i = 0; i < store.length; i++) {
if (store[i] !== 0) return false;
}

return true;
}