Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added new urlify method #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions chapter01/1.1 - Is Unique/isUnique-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const isUniqueCharacters = (input) => {
if (input.length === 0) {
return true;
}
// Based on ASCII string
if (input.length > 128) {
return false;
}

const uniqueCharacters = {};

for (i = 0; i < input.length; i++) {
const character = input[i];
if (uniqueCharacters[character]) {
return false;
}

if (!uniqueCharacters[character]) {
uniqueCharacters[character] = 1;
}
}

return true;
};

console.log(isUniqueCharacters("abcde"));
console.log(isUniqueCharacters("abcdefghh"));
console.log(isUniqueCharacters(""));
33 changes: 33 additions & 0 deletions chapter01/1.2 - Check Perm/checkPermute-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const checkPermute = (stringA, stringB) => {
if (stringA.length !== stringB.length) {
return false;
}
const letters = {};
for (i = 0; i < stringA.length; i++) {
const letter = stringA[i];
if (!letters[letter]) {
letters[letter] = 1;
} else {
letters[letter] += 1;
}
}

for (i = 0; i < stringB.length; i++) {
const letter = stringB[i];
if (!letters[letter]) {
return false;
}
letters[letter] -= 1;

if (letters[letter] < 0) {
return false;
}
}

return true;
};

console.log(checkPermute("a", "bbbb"));
console.log(checkPermute("aba", "baa"));
console.log(checkPermute("aaaa", "bbbb"));
console.log(checkPermute(" a", "a "));
10 changes: 10 additions & 0 deletions chapter01/1.3 - URLify/urlify-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const urlify = (input) => {
return [...input].reduce((previousValue, character) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using inbuilt func, reduce.

if (character === " ") {
return (previousValue += "%20");
}
return (previousValue += character);
}, "");
};

console.log(urlify("Mr John Smith"), "Mr%20John%20Smith");