Skip to content

Commit efe5149

Browse files
author
krystal chen
committed
1,2,3
1 parent 05e701b commit efe5149

File tree

5 files changed

+725
-0
lines changed

5 files changed

+725
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
function everyCharUnique(str){
3+
let map = {};
4+
str.split('').forEach((ele, index) => {
5+
6+
if(map[ele] === undefined){
7+
map[ele] = 0;
8+
// console.log(map);
9+
} else {
10+
map[ele]+=1;
11+
}
12+
});
13+
14+
// console.log(map);
15+
for(let key in map){
16+
// console.log(key)
17+
// console.log(map[key]);
18+
if(map[key] !== 0){
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
25+
26+
/* TESTS */
27+
console.log(everyCharUnique('abcd'), 'true');
28+
console.log(everyCharUnique('abccd'), 'false');
29+
console.log(everyCharUnique('bhjjb'), 'false');
30+
console.log(everyCharUnique('mdjq'), 'true');
31+
32+
33+
34+
35+
function everyCharUnique1(str){
36+
37+
for(let a = 0; a < str.length; a+=1){
38+
for(let b = a+1; b < str.length; b+=1){
39+
if(str[a] === str[b]){
40+
return false;
41+
}
42+
}
43+
}
44+
return true;
45+
}
46+
47+
48+
/* TESTS */
49+
console.log(everyCharUnique1('abcd'), 'true');
50+
console.log(everyCharUnique1('abccd'), 'false');
51+
console.log(everyCharUnique1('bhjjb'), 'false');
52+
console.log(everyCharUnique1('mdjq'), 'true');
53+
54+
55+
function everyCharUnique2(str){
56+
57+
let obj = {};
58+
59+
for(let a = 0; a < str.length; a+=1){
60+
61+
if(obj[str[a]] && obj[str[a]] === 1){
62+
return false;
63+
} else {
64+
obj[str[a]] = 1;
65+
}
66+
}
67+
68+
console.log(obj);
69+
return true;
70+
}
71+
72+
73+
/* TESTS */
74+
console.log(everyCharUnique2('abcd'), 'true');
75+
console.log(everyCharUnique2('abccd'), 'false');
76+
console.log(everyCharUnique2('bhjjb'), 'false');
77+
console.log(everyCharUnique2('mdjq'), 'true');
78+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
3+
function checkPermute(str1, str2){
4+
5+
if(str1.length !== str2.length) return false;
6+
7+
let comparisonA = 0;
8+
9+
str1 = str1.split('').sort((a, b) => {
10+
11+
if(a < b){
12+
comparisonA = -1;
13+
} else {
14+
comparisonA = 1;
15+
}
16+
return comparisonA;
17+
})
18+
19+
// console.log(str1)
20+
str2 = str2.split('').sort((a, b) => a.localeCompare(b));
21+
// console.log(str2);
22+
23+
24+
let a = 0;
25+
let b = 0;
26+
27+
while(a < str1.length && b < str2.length){
28+
if(str1[a] === str2[b]){
29+
a+=1;
30+
b+=1;
31+
} else {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
// Tests
39+
console.log(checkPermute('aba', 'aab'), true);
40+
console.log(checkPermute('aba', 'bab'), false);
41+
console.log(checkPermute('aba', 'aaba'), false);
42+
console.log(checkPermute('aba', 'aa'), false);
43+
44+
45+
function checkPermute1(str1, str2){
46+
let newMap = {};
47+
48+
if(str1.length < str2.length){
49+
let temp = str1;
50+
str1 = str2;
51+
str2 = temp;
52+
}
53+
54+
55+
for(let a = 0; a < str1.length; a+=1){
56+
if(!newMap[str1[a]]){
57+
newMap[str1[a]] = 1;
58+
} else{
59+
newMap[str1[a]] +=1;
60+
}
61+
}
62+
63+
console.log(newMap);
64+
65+
for(let b = 0; b < str2.length; b+=1){
66+
if(newMap[str2[b]]){
67+
newMap[str2[b]] -=1;
68+
}
69+
}
70+
71+
for(let key in newMap){
72+
if(newMap[key] > 0){
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
79+
// Tests
80+
console.log(checkPermute1('aba', 'aab'), true);
81+
console.log(checkPermute1('aba', 'bab'), false);
82+
console.log(checkPermute1('aba', 'aaba'), false);
83+
console.log(checkPermute1('aba', 'aa'), false);
84+
85+
86+
function checkPermute2(str1, str2){
87+
if(str1.length !== str2.length) return false;
88+
return str1.split('').sort().join('') === str2.split('').sort().join('');
89+
}
90+
91+
// Tests
92+
console.log(checkPermute2('aba', 'aab'), true);
93+
console.log(checkPermute2('aba', 'bab'), false);
94+
console.log(checkPermute2('aba', 'aaba'), false);
95+
console.log(checkPermute2('aba', 'aa'), false);
96+
97+
98+
function checkPermute3(str1, str2){
99+
if(str1.length !== str2.length) return false;
100+
return String(str1).split('').sort().join('') === String(str2).split('').sort().join('');
101+
}
102+
103+
// Tests
104+
console.log(checkPermute3(123, 321), true);
105+
106+

chapter01/1.3 - URLify/1.urlify.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
function urlify(str, length){
4+
5+
let strArr = str.split('');
6+
let spaceCount = 0;
7+
8+
for(let word of strArr){
9+
if(word === ' '){
10+
spaceCount+=1;
11+
}
12+
}
13+
14+
let oldLength = strArr.length;
15+
let newLength = length + spaceCount * 2;
16+
17+
console.log(oldLength);
18+
console.log(newLength);
19+
20+
21+
for(let a = length - 1; a >= 0; a--){
22+
console.log(newLength);
23+
if(strArr[a] === ' '){
24+
console.log(strArr[a], a);
25+
console.log(strArr[newLength], newLength);
26+
strArr[newLength - 1] = '0';
27+
strArr[newLength - 2] = '2';
28+
strArr[newLength - 3] = '%';
29+
newLength -=3;
30+
} else {
31+
strArr[newLength - 1] = strArr[a];
32+
newLength -=1;
33+
console.log(strArr[a], newLength)
34+
console.log(strArr[newLength])
35+
}
36+
}
37+
return strArr.join('');
38+
39+
}
40+
console.log(urlify('Mr John Smith', 13), 'Mr%20John%20Smith');
41+
42+
43+
44+
function urlify2(str, length){
45+
46+
let strArr = str.split('');
47+
let additional = 0;
48+
49+
for(let char of strArr){
50+
if(char === ' '){
51+
additional+=1;
52+
}
53+
}
54+
55+
let oldLength = length;
56+
let newLength = length + additional* 2;
57+
58+
console.log(oldLength);
59+
console.log(newLength);
60+
61+
for(let a = length - 1; a >= 0; a-=1){
62+
63+
if(strArr[a] === ' '){
64+
strArr[newLength - 1] = '0';
65+
strArr[newLength - 2] = '2';
66+
strArr[newLength - 3] = '%';
67+
newLength-=3;
68+
} else {
69+
strArr[newLength - 1] = strArr[a];
70+
newLength-=1;
71+
}
72+
}
73+
return strArr.join('');
74+
}
75+
76+
console.log(urlify2('Mr John Smith', 13), 'Mr%20John%20Smith');
77+
78+
79+
80+
function replaceUrlSpaces(str){
81+
return str.replaceAll(' ', '%20');
82+
}
83+
console.log(replaceUrlSpaces("Sai Charan P"));
84+
85+
86+
function replaceUrlSpaces1(str){
87+
let strArr = str.trim().split('');
88+
89+
}
90+
console.log(replaceUrlSpaces1("Sai Charan P"));

chapter01/1.3 - URLify/urlify.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ var urlify = function(str, length) {
22
// have a pointer to check from start to end
33
var strArr = str.split('');
44
var pointer = 0;
5+
56
while (pointer < str.length) {
67
if (strArr[pointer] === ' ') {
78
// *** needs more work here, a little wierd
89
// not handling trailing spaces properly
910
for (var i = str.length - 1; i > pointer + 3; i--) {
1011
strArr[i] = str[i - 2];
1112
}
13+
1214
strArr[pointer] = '%';
1315
strArr[pointer+1] = '2';
1416
strArr[pointer+2] = '0';

0 commit comments

Comments
 (0)