Skip to content

Commit 876b36e

Browse files
committed
Solve section6
1 parent 8d945e3 commit 876b36e

12 files changed

+268
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const validAnagram = (str1, str2) => {
2+
const freq1 = {};
3+
const freq2 = {};
4+
5+
for (let letter of str1) {
6+
freq1[letter] = (freq1[letter] || 0) + 1;
7+
}
8+
9+
for (let letter of str2) {
10+
freq2[letter] = (freq2[letter] || 0) + 1;
11+
}
12+
13+
for (let key in freq1) {
14+
if (!(key in freq2)) {
15+
return false;
16+
}
17+
18+
if (freq1[key] !== freq2[key]) {
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
};
25+
26+
console.log(validAnagram('true', 'turee'));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const countUniqueValues = (arr) => {
2+
let i = 0;
3+
let j = 1;
4+
5+
if (arr.length === 0) {
6+
return 0;
7+
}
8+
9+
while (j < arr.length) {
10+
if (arr[i] !== arr[j]) {
11+
i++;
12+
arr[i] = arr[j];
13+
}
14+
15+
j++;
16+
}
17+
console.log(i + 1);
18+
19+
return i + 1;
20+
};
21+
22+
countUniqueValues([1, 1, 1, 1, 1, 2]); // 2
23+
countUniqueValues([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13]); // 7
24+
countUniqueValues([]); // 0
25+
countUniqueValues([-2, -1, -1, 0, 1]);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function maxSubarraySum(arr, num) {
2+
if ( num > arr.length){
3+
return null;
4+
}
5+
var max = -Infinity;
6+
for (let i = 0; i < arr.length - num + 1; i ++){
7+
temp = 0;
8+
for (let j = 0; j < num; j++){
9+
temp += arr[i + j];
10+
}
11+
if (temp > max) {
12+
max = temp;
13+
}
14+
}
15+
return max;
16+
}
17+
18+
maxSubarraySum([2,6,9,2,1,8,5,6,3],3)
19+
20+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function maxSubarraySum(arr, num) {
2+
let maxSum = 0;
3+
let tempSum = 0;
4+
if (arr.length < num) return null;
5+
6+
for (let i = 0; i < num; i++) {
7+
maxSum += arr[i];
8+
}
9+
10+
tempSum = maxSum;
11+
12+
for (let i = num; i < arr.length; i++) {
13+
tempSum = tempSum - arr[i - num] + arr[i];
14+
maxSum = Math.max(maxSum, tempSum);
15+
}
16+
17+
return maxSum;
18+
}
19+
20+
console.log(maxSubarraySum([2, 6, 9, 2, 1, 8, 5, 6, 3], 3));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function countUniqueValues(arr){
2+
if(arr.length === 0) return 0;
3+
var i = 0;
4+
for(var j = 1; j < arr.length; j++){
5+
if(arr[i] !== arr[j]){
6+
i++;
7+
arr[i] = arr[j]
8+
}
9+
}
10+
return i + 1;
11+
}
12+
countUniqueValues([1,2,2,5,7,7,99])

section6/areThereDuplicates.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const areThereDuplicates = (...array) => {
2+
const freq1 = {};
3+
4+
for (let val of array) {
5+
freq1[val] = (freq1[val] || 0) + 1;
6+
}
7+
8+
for (let key in freq1) {
9+
if (freq1[key] > 1) {
10+
return true;
11+
}
12+
}
13+
14+
return false;
15+
};
16+
17+
console.log(areThereDuplicates('a', 'b', 'c', 'a'));

section6/averagePair.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const averagePair = (arr, num) => {
2+
let start = 0;
3+
let end = arr.length - 1;
4+
5+
while (start < end) {
6+
let avg = (arr[start] + arr[end]) / 2;
7+
if (avg === num) {
8+
return true;
9+
} else if (avg < num) {
10+
start++;
11+
} else {
12+
end--;
13+
}
14+
}
15+
16+
return false;
17+
};
18+
19+
console.log(averagePair([1, 2, 3], 2.5));
20+
// console.log(averagePair([1, 3, 3, 5, 6, 7, 10, 12, 19], 8));

section6/findLongestSubstring.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const findLongestSubstring = (str) => {
2+
let longest = 0;
3+
let seen = {};
4+
let start = 0;
5+
6+
for (let i = 0; i < str.length; i++) {
7+
let char = str[i];
8+
if (seen[char]) {
9+
start = Math.max(start, seen[char]);
10+
}
11+
// index - beginning of substring + 1 (to include current in count)
12+
longest = Math.max(longest, i - start + 1);
13+
// store the index of the next char so as to not double count
14+
seen[char] = i + 1;
15+
}
16+
return longest;
17+
};
18+
19+
console.log(findLongestSubstring('bbbbbb'));

section6/isSubsequence.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const isSubsequence = (str1, str2) => {
2+
let i = 0;
3+
let j = 0;
4+
5+
if (!str1) return true;
6+
7+
while (j < str2.length) {
8+
if (str1[i] === str2[j]) {
9+
i++;
10+
}
11+
12+
if (i === str1.length) {
13+
return true;
14+
}
15+
16+
j++;
17+
}
18+
19+
return false;
20+
};
21+
22+
console.log(isSubsequence('abc', 'acb'));

section6/maxSubarraySum.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const maxSubarraySum = (arr, num) => {
2+
let maxSum = 0;
3+
let tempSum = 0;
4+
5+
if (num > arr.length) {
6+
return null;
7+
}
8+
9+
for (let i = 0; i < num; i++) {
10+
maxSum += arr[i];
11+
}
12+
13+
tempSum = maxSum;
14+
15+
for (let i = num; i < arr.length; i++) {
16+
tempSum = tempSum - arr[i - num] + arr[i];
17+
18+
maxSum = Math.max(tempSum, maxSum);
19+
}
20+
21+
return maxSum;
22+
};
23+
24+
console.log(maxSubarraySum([2, 3], 3));

0 commit comments

Comments
 (0)