Skip to content

Instantly share code, notes, and snippets.

@lienista
lienista / ctci-1.1-string-has-all-unique-characters.js
Last active August 28, 2018 07:44
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
const stringHasUniqueChars = (str) => {
for(let i=0;i<str.length; i++) {
for(let j=i+1;j<str.length; j++) {
if(str[i] === str[j])
return false;
}
}
return true;
}
@lienista
lienista / ctci-1.1-string-has-all-unique-characters-1.js
Last active August 28, 2018 07:44
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
const stringHasUniqueChars = (str) => {
str.split('').sort().join('');
for(let i=0;i<str.length; i++) {
if(str[i] === str[i+1]) {
return false;
}
}
return true;
}
@lienista
lienista / ctci-1.1-string-has-all-unique-characters-2.js
Last active August 28, 2018 07:43
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
const stringHasUniqueChars = (str) => {
let count = {};
for(let i=0; i<str.length; i++) {
count[str[i]] = count[str[i]] === undefined ? 1: count[str[i]]+1;
if(count[str[i]] > 1)
return false;
}
return true;
}
@lienista
lienista / ctci-1.1-string-has-all-unique-characters-3.js
Last active August 28, 2018 07:43
Algorithms in Javascript: CTCI 1.1 - Is Unique: Write a function to determine if a string has all unique characters.
const stringHasUniqueChars = (str) => {
let checker = 0; //integer used to represent 32-bit operator
for (let i = 0; i < str.length; i++) {
let bitAtIndex = str[i] - 'a';
if ((checker & (1 << bitAtIndex)) > 0) {
return false;
}
checker = checker | (1<<bitAtIndex);
}
return true;
@lienista
lienista / ctci-1.4-is-string-a-permutation.js
Last active August 28, 2018 07:41
Algorithms in Javascript: CTCI 1.4 Palindrome Permutation: Given a string, determine if it is a permutation of a palindrome.
const isPermutation = (s1, s2) => {
if(s1.length != s2.length) return false;
return s1.split('').sort().join('') === s2.split('').sort().join('');
}
let a = isPermutation("paypalishiring", "palpayhiringis"); // true
let b = isPermutation("paypalishiring", "hiringpalsipay"); // true
let c = isPermutation("paypalishiring", "palpayhirising"); // true
let d = isPermutation("paypalishiring", "palpayhiringgg"); // false
console.log(a + ', ' + b + ', ' + c + ', ' + d);
@lienista
lienista / ctci-1.4-is-string-a-permutation-1.js
Last active August 28, 2018 07:40
Algorithms in Javascript: CTCI 1.4 Palindrome Permutation: Given a string, determine if it is a permutation of a palindrome.
const isPermutation = (s1, s2) => {
if(s1.length != s2.length) return false;
let count = {};
for(let i=0; i < s1.length; i++) {
count[s1[i]] = count[s1[i]] === undefined ? 1: count[s1[i]] + 1;
count[s2[i]] = count[s2[i]] === 0 ? 0 : count[s2[i]] - 1;
}
@lienista
lienista / ctci-1.4-is-string-a-permutation-2.js
Last active August 28, 2018 07:40
Algorithms in Javascript: CTCI 1.4 Palindrome Permutation: Given a string, determine if it is a permutation of a palindrome.
const isPermutation = (s1, s2) => {
if(s1.length != s2.length) return false;
let count = new Map();
for (let i = 0; i < s1.length; ++i) {
count.set(s1[i], count.get(s1[i]) + 1 || 1);
}
for(let j = 0; j < s2.length; ++j) {
let num = count.get(s2[j]);
@lienista
lienista / ctci-1.3-urlify.js
Last active August 28, 2018 07:36
Algorithms in Javascript: CTCI 1.3 URLify: Write a function to create an URL by replacing all spaces of an URL string with %20
const createUrl = (s1) => {
return s1.split(' ').join('%20');
}
let url = 'javascript algorithms'
console.log(createUrl(url));
@lienista
lienista / ctci-1.3-urlify-1.js
Last active August 28, 2018 07:37
Algorithms in Javascript: CTCI 1.3 URLify: Write a function to create an URL by replacing all spaces of an URL string with %20
const createUrl = (s1) => {
let s1Array = s1.split(' ');
let len = s1Array.length - 1;
if(s1Array[len] === '') {
s1Array.pop();
s1 = s1Array.join(' ');
return createUrl(s1);
}
return s1Array.join('%20');
}
@lienista
lienista / ctci-1.4-is-string-a-palindrome.js
Last active August 28, 2018 07:38
Algorithms in Javascript: CTCI 1.4 Palindrome Permutation: Given a string, determine if it is a permutation of a palindrome.
const isPalindrome = (s1) => {
let charMap = new Map();
for(let i=0; i <s1.length; i++){
if(charMap.get(s1[i])) {
charMap.delete(s1[i]);
} else {
charMap.set(s1[i], 1);
}
}
if(s1.length%2 === 0 && charMap.size === 0 ||