-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41f5450
commit 1dcce09
Showing
5 changed files
with
184 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
Complete the alternatingCharacters function in the editor below. It must return an integer representing the minimum number of deletions to make the alternating string. | ||
Output Format | ||
For each query, print the minimum number of deletions required on a new line. | ||
Sample Input | ||
5 | ||
AAAA | ||
BBBBB | ||
ABABABAB | ||
BABABA | ||
AAABBB | ||
Sample Output | ||
3 | ||
4 | ||
0 | ||
0 | ||
4 | ||
*/ | ||
|
||
const alternatingCharacters = (s) => { | ||
let count = 0; | ||
|
||
for (let i = 0; i < s.length - 1; i++) { | ||
const currChar = s.charAt(i); | ||
const nextChar = s.charAt(i + 1); | ||
if (currChar === nextChar) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
}; | ||
|
||
console.log(alternatingCharacters("AAAA")); // A___ = 3 Deletions | ||
console.log(alternatingCharacters("BBBBB")); // B____ = 4 Deletions | ||
console.log(alternatingCharacters("ABABABAB")); // ABABABAB = 0 Deletions | ||
console.log(alternatingCharacters("BABABA")); // BABABA = 0 Deletions | ||
console.log(alternatingCharacters("AAABBB")); // A__B__ = 4 Deletions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
You are given an array and you need to find number of tripets of indices (i, j, k) such that the elements at those indices are in geometric progression for a given common ratio r and i < j < k.] | ||
For example, arr = [1,4,16,64]. If r=4, we have [1,4,16] and [4,16,64] at indices (0,1,2) and (1,2,3). | ||
Sample Input 0 | ||
4 2 | ||
1 2 2 4 | ||
Sample Output 0 | ||
2 | ||
Explanation 0 | ||
There are | ||
triplets in satisfying our criteria, whose indices are and | ||
Sample Input 1 | ||
6 3 | ||
1 3 9 9 27 81 | ||
Sample Output 1 | ||
6 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to a target value. | ||
For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values meeting the condition: | ||
2 - 1 =1, 3 - 2 = 1, and 4 - 3 = 1. | ||
Sample Input | ||
5 2 | ||
1 5 3 4 2 | ||
Sample Output | ||
3 | ||
Explanation | ||
There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2] and [3,1] . | ||
Source: https://www.hackerrank.com/challenges/pairs/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search | ||
*/ | ||
|
||
// Brute force O(n^2) | ||
// function pairs(k, arr) { | ||
// let numPairs = 0; | ||
|
||
// for (let i = 0; i < arr.length; i++) { | ||
// for (let j = 0; j < arr.length; j++) { | ||
// if (arr[i] - arr[j] === k) { | ||
// numPairs++; | ||
// } | ||
// } | ||
// } | ||
|
||
// return numPairs; | ||
// } | ||
|
||
// Fast solution - O(n) | ||
function pairs(k, arr) { | ||
let cache = {}; | ||
let count = 0; | ||
|
||
// Iterate through the array once, and add each number to a dictionary. | ||
arr.forEach((num) => { | ||
cache[num] = num; | ||
}); | ||
|
||
// iterate through a second time, and, for each number, see if that number + k exists in the library. If it does, then you know you have a pair, and can increment anwser | ||
arr.forEach((num) => { | ||
const sum = num + k; | ||
if (sum in cache) { | ||
count++; | ||
} | ||
}); | ||
|
||
return count; | ||
} | ||
|
||
// console.log(pairs(1, [1, 2, 3, 4])); // 3 | ||
console.log(pairs(2, [1, 5, 3, 4, 2])); // 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
Print a single integer denoting the number of letter a's in the first letters of the infinite string created by repeating infinitely many times. | ||
Sample Input 0 | ||
aba | ||
10 | ||
Sample Output 0 | ||
7 | ||
*/ | ||
function repeatedString(s, n) { | ||
let count = 0; | ||
let reminderCount = 0; | ||
let remainder = n % s.length; | ||
|
||
for (let i = s.length; i-- > 0; ) { | ||
if (s.charAt(i) == "a") { | ||
++count; | ||
|
||
if (i < remainder) ++reminderCount; | ||
} | ||
} | ||
|
||
return ((n - remainder) / s.length) * count + reminderCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters