-
Notifications
You must be signed in to change notification settings - Fork 0
/
what.js
96 lines (85 loc) · 2.1 KB
/
what.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function lengthCounter(string) {
let i = 0;
for (str in string) {
i++
}
return i
}
module.exports = lengthCounter;
function reverse(string) {
let answer = ''
for (let i = string.length - 1; i >= 0; i--) {
answer += string[i];
} return answer
}
// console.log(reverse('asdfghj'))
function shifter(array, number) {
let answer = [];
for (let i = array.length - number; i < array.length; i++) {
answer.push(array[i])
}
for (let j = 0; j < array.length - number; j++) {
answer.push(array[j]);
}
return answer
}
console.log(shifter([1, 2, 3, 4, 5], 1))
// 1
// function maximumOccurringCharacter(text) {
// // Write your code here
// let maxOccuredCharacter = '';
// let ammuntItOccured = 0;
// let allChars = {};
// for (let i = 0; i< text.length; i++){
// if (text[i] in allChars){
// allChars[text[i]]++;
// } else {
// allChars[text[i]] = 1;
// }
// }
// for (let char in allChars){
// if (allChars[char] > ammuntItOccured){
// ammuntItOccured = allChars[char];
// maxOccuredCharacter = char;
// }
// }
// return maxOccuredCharacter;
// }
// 2
function shortestSubstring(s) {
// Write your code here
let uniqueCharacters = {};
for (let i = 0; i < s.length; i++) {
if (s[i] in uniqueCharacters) {
uniqueCharacters[s[i]]++;
} else {
uniqueCharacters[s[i]] = 1;
}
}
let objKeys = Object.keys(uniqueCharacters);
let startIndex = 0;
let isMatch = false;
let uniqueCharsLength = Object.keys(uniqueCharacters).length;
let subStr = '';
while (uniqueCharsLength <= s.length) {
subStr = s.substr(startIndex, uniqueCharsLength);
if (subStr.length === uniqueCharsLength) {
match = true;
for (i = 0; i < uniqueCharsLength; i++) {
let val = objKeys[i];
if (subStr.indexOf(val) === -1) {
isMatch = false;
break;
}
}
if (isMatch) {
return subStr.length;
}
startIndex++;
} else {
startIndex = 0;
uniqueCharsLength++;
}
}
}
console.log(shortestSubstring('aabcce'))