Skip to content

Instantly share code, notes, and snippets.

View sAVItar02's full-sized avatar
☢️

Aviral Singh Chauhan sAVItar02

☢️
  • Bangalore, India
View GitHub Profile
@sAVItar02
sAVItar02 / generateParentheses.js
Created November 25, 2024 13:33
Generate Parentheses
/**
* @param {number} n
* @return {string[]}
*/
var generateParenthesis = function(n) {
let res = [];
function rec(open, close, string = "") {
if((open == close) && (open + close == 2*n)) {
res.push(string);
@sAVItar02
sAVItar02 / permutations.js
Created November 24, 2024 05:15
Permutations
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums, res = [], temp = []) {
if(nums.length === 0) res.push([...temp]);
for(let i=0; i<nums.length; i++) {
// Choose
temp.push(nums[i]);
@sAVItar02
sAVItar02 / subsets.js
Created November 21, 2024 06:47
Subsets
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
let temp = [];
let res = [];
function rec(nums, i = 0) {
if(i === nums.length ) return res.push([...temp]);
@sAVItar02
sAVItar02 / palindromeNumber.js
Created November 19, 2024 03:32
Palindrome Number
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if(x < 0) return false;
function rec(num, rev = 0) {
if(num == 0) return rev;
return rec(Math.floor(num / 10), (rev * 10 + (num % 10)))
@sAVItar02
sAVItar02 / summaryRanges.js
Created November 18, 2024 08:00
Summary Ranges
/**
* @param {number[]} nums
* @return {string[]}
*/
var summaryRanges = function(nums) {
let arr = [];
let start = nums[0];
for(let i=0; i<nums.length; i++) {
if(nums[i+1] - nums[i] == 1) continue;
@sAVItar02
sAVItar02 / factorialTrailingZeroes.js
Created November 16, 2024 12:20
Factorial Trailing Zeroes
/**
* @param {number} n
* @return {number}
*/
var trailingZeroes = function(n) {
let x = 5;
let zeroes = 0;
while((n/x) >= 1) {
zeroes += Math.floor(n / x);
x = x * 5;
@sAVItar02
sAVItar02 / fibonacciNumber.js
Created November 14, 2024 17:35
Fibonacci Number
/**
* @param {number} n
* @return {number}
*/
var fib = function(n) {
if(n == 0) return 0;
if(n == 1) return 1;
return fib(n - 1) + fib(n - 2);
};
@sAVItar02
sAVItar02 / longestSubstringWithoutRepeating.js
Created November 13, 2024 12:10
Longest Substring Without Repeating Characters
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let verifier = {};
let i = 0;
let j = i+1;
let max = 1;
@sAVItar02
sAVItar02 / stringToInteger.js
Created November 12, 2024 03:57
String to integer (atoi)
/**
* @param {string} s
* @return {number}
*/
var myAtoi = function(s) {
let num = 0;
let isNegative = false;
let numString = "";
let hasEncountered = false;
let hasBeenPositive = false;
@sAVItar02
sAVItar02 / longestCommonPrefix.js
Created November 11, 2024 06:11
Longest Common Prefix
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
if(strs.length == 0) return "";
if(strs.length == 1) return strs[0];
let prefix = strs[0];
let prefixLen = prefix.length;