From 1dcce09c2efe3016f089a7dd95da41256ce22165 Mon Sep 17 00:00:00 2001 From: Joe Karlsson Date: Tue, 21 Jul 2020 19:03:25 -0500 Subject: [PATCH] finished alternating strings --- alternatingCharacters.js | 44 +++++++++++++++++++++++++ countTriplets.js | 30 +++++++++++++++++ pairs.js | 62 +++++++++++++++++++++++++++++++++++ repeatedStrings.js | 28 ++++++++++++++++ sherlockAndAnagrams.js | 71 +++++++++++----------------------------- 5 files changed, 184 insertions(+), 51 deletions(-) create mode 100644 alternatingCharacters.js create mode 100644 countTriplets.js create mode 100644 pairs.js create mode 100644 repeatedStrings.js diff --git a/alternatingCharacters.js b/alternatingCharacters.js new file mode 100644 index 0000000..a00d472 --- /dev/null +++ b/alternatingCharacters.js @@ -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 diff --git a/countTriplets.js b/countTriplets.js new file mode 100644 index 0000000..9025413 --- /dev/null +++ b/countTriplets.js @@ -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 + + + */ \ No newline at end of file diff --git a/pairs.js b/pairs.js new file mode 100644 index 0000000..4468001 --- /dev/null +++ b/pairs.js @@ -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 diff --git a/repeatedStrings.js b/repeatedStrings.js new file mode 100644 index 0000000..85e49f8 --- /dev/null +++ b/repeatedStrings.js @@ -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; +} diff --git a/sherlockAndAnagrams.js b/sherlockAndAnagrams.js index 3dbc3b4..bbefb56 100644 --- a/sherlockAndAnagrams.js +++ b/sherlockAndAnagrams.js @@ -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