Skip to content

Commit

Permalink
finished alternating strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarlsson committed Jul 22, 2020
1 parent 41f5450 commit 1dcce09
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 51 deletions.
44 changes: 44 additions & 0 deletions alternatingCharacters.js
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
30 changes: 30 additions & 0 deletions countTriplets.js
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
*/
62 changes: 62 additions & 0 deletions pairs.js
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
28 changes: 28 additions & 0 deletions repeatedStrings.js
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;
}
71 changes: 20 additions & 51 deletions sherlockAndAnagrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,29 @@ Sample Output 0
*
*/

const findAllSubStrings = (s) => {
let allSubStrings = [];

// Loop through all of the substrings of s O(n^2)
for (let i = 0; i < s.length; i++) {
for (let j = i + 1; j < s.length + 1; j++) {
allSubStrings.push(s.slice(i, j));
}
}
return allSubStrings;
};

const duplicatesInArr = (arr) => {
arr.forEach((el) => {
counts[el] = (counts[el] || 0) + 1;
});
return counts;
};

const reverseString = (str) => {
return str.split("").reverse().join("");
};
reverseString("hello");

function sherlockAndAnagrams(s) {
let subStrings = {};
let result = 0;

// Loop through all of the substrings of s O(n^2)
for (let i = 0; i < s.length; i++) {
for (let j = i + 1; j < s.length + 1; j++) {
const subString = s.slice(i, j);
if (!(subString in subStrings)) {
subStrings[subString] = 1;
// check if the revese has been added yet
const reverseSubStr = reverseString(subString);

if (
reverseSubStr.length > 1 &&
reverseSubStr.length < s.length &&
reverseSubStr in subStrings
) {
subStrings[subString]++;
}
} else {
// substring already in chache
subStrings[subString]++;
}
let anagrams = 0;

for (let x = 1; x < s.length + 1; x++) {
const stringMap = {};

for (let y = 0; y < s.length - x + 1; y++) {
const string = s
.substring(y, y + x)
.split("")
.sort()
.join("");
stringMap[string] = (stringMap[string] || 0) + 1;
}
console.log(stringMap);

Object.values(stringMap).forEach((value) => {
anagrams += (value * (value - 1)) / 2;
});
}
console.log(subStrings);
return result;
return anagrams;
}

// console.log(sherlockAndAnagrams("abba")); // 4
console.log(sherlockAndAnagrams("abba")); // 4
// console.log(sherlockAndAnagrams("abcd")); // 0
console.log(sherlockAndAnagrams("kkkk")); // 10
// console.log(sherlockAndAnagrams("kkkk")); // 10

0 comments on commit 1dcce09

Please sign in to comment.