Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JoeKarlsson/programming-problems
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarlsson committed Mar 12, 2019
2 parents 3d60b99 + 09e876d commit 371ca0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# programming-problems

Solutions for various programming problems


More problems can be found here: https://github.com/mre/the-coding-interview
30 changes: 30 additions & 0 deletions src/subStringsKDist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function areDistinctChars(chars) {
const charCache = {};

for (let i = 0; i < chars.length; i++) {
if (charCache[chars[i]] !== undefined) {
return false;
} else {
charCache[chars[i]] = 0;
}
}
return true;
}

function subStringsKDist(inputStr, kDistinctLettters) {
const chars = inputStr.split("");
const subStrings = {};

for (let i = 0; i < chars.length; i++) {
const subArray = chars.slice(i, i + kDistinctLettters);
if (subArray.length === kDistinctLettters) {
if (areDistinctChars(subArray)) {
subStr = subArray.join("");
if (subStrings[subStr] === undefined) {
subStrings[subStr] = 0;
}
}
}
}
return Object.keys(subStrings);
}

0 comments on commit 371ca0d

Please sign in to comment.