-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanswer.js
More file actions
29 lines (25 loc) · 748 Bytes
/
Copy pathanswer.js
File metadata and controls
29 lines (25 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Time : 10:47
const kdistinct = (string, k) => {
const allSubString = [];
let longestSubstring = "";
for(let i=0; i < string.length; i++) {
let currentSubString = "";
let distinctCounter = 0;
for(let j=i; j < string.length; j++) {
if(!currentSubString.includes(string.charAt(j))) {
distinctCounter++;
if(distinctCounter > k) {
break;
}
}
currentSubString += string.charAt(j);
}
longestSubstring = currentSubString.length > longestSubstring.length ? currentSubString : longestSubstring;
allSubString.push(currentSubString);
}
console.log({ allSubString });
return longestSubstring;
}
const s = "abcba";
const k =2;
console.log(kdistinct(s, k))