-
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
4b31669
commit 41f5450
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,81 @@ | ||
/** | ||
* | ||
Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. Given a string, find the number of pairs of substrings of the string that are anagrams of each other. | ||
For example s = mom, the list of all anagrammatic pairs is [m,m], [mo,om] at positions [[0],[2]], [[0],[1], [1,2]] respectively. | ||
Output Format | ||
For each query, return the number of unordered anagrammatic pairs. | ||
Sample Input 0 | ||
2 | ||
abba | ||
abcd | ||
Sample Output 0 | ||
4 | ||
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]++; | ||
} | ||
} | ||
} | ||
console.log(subStrings); | ||
return result; | ||
} | ||
|
||
// console.log(sherlockAndAnagrams("abba")); // 4 | ||
// console.log(sherlockAndAnagrams("abcd")); // 0 | ||
console.log(sherlockAndAnagrams("kkkk")); // 10 |
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,59 @@ | ||
/** | ||
Given two strings, determine if they share a common substring. A substring may be as small as one character. | ||
For example, the words "a", "and", "art" share the common substring a. The words "be" and "cat" do not share a substring. | ||
Function Description | ||
Complete the function twoStrings in the editor below. It should return a string, either YES or NO based on whether the strings share a common substring. | ||
twoStrings has the following parameter(s): | ||
s1, s2: two strings to analyze . | ||
Input Format | ||
The first line contains a single integer i, | ||
Output Format | ||
For each pair of strings, return YES or NO. | ||
Sample Input | ||
2 | ||
hello | ||
world | ||
hi | ||
world | ||
Sample Output | ||
YES | ||
NO | ||
**/ | ||
|
||
const twoString = (s1, s2) => { | ||
const subStringCache = {}; | ||
const arr1 = s1.split(""); | ||
const arr2 = s2.split(""); | ||
let subStringExists = "NO"; | ||
|
||
// Build up cache for s1 O(n) | ||
arr1.forEach((letter) => { | ||
if (!(letter in subStringCache)) { | ||
subStringCache[letter] = true; | ||
} | ||
}); | ||
|
||
// check if str2 is in the cache? | ||
console.log(subStringCache); | ||
arr2.forEach((letter) => { | ||
if (letter in subStringCache) { | ||
subStringExists = "YES"; | ||
break; | ||
} | ||
}); | ||
return subStringExists; | ||
}; | ||
|
||
// Examples | ||
console.log('twoString("hello", "world")', twoString("hello", "world")); // YES | ||
console.log('twoString("hi", "world")', twoString("hi", "world")); // NO |