-
Notifications
You must be signed in to change notification settings - Fork 0
/
interviewprep.js
100 lines (86 loc) · 2.19 KB
/
interviewprep.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
97
98
99
100
'use strict'
// String manipulation
// making anagrams
function makeAnagram(string1, string2) {
let counter = 0;
let characters = {};
for (let char of string1) {
characters[char] = (characters[char] || 0) + 1;
}
for (let char of string2) {
characters[char] = (characters[char] || 0) - 1;
}
for (let num in characters) {
counter += Math.abs(characters[num])
}
return counter;
}
// console.log(makeAnagram("cde", "abc"))
// altering characters
function alternatingCharacters(s) {
let counter = 0;
let outputString = '';
for (let i = 0; i < s.length; i++) {
if (s[i] === outputString[outputString.length - 1]) {
counter++
} else {
outputString += s[i];
}
}
return counter
}
// alternatingCharacters('AAAA')
// alternatingCharacters('BBB')
// alternatingCharacters('ABABABAB')
// alternatingCharacters('AAABBB')
// sherlock and the valid string
function isValid(s) {
let characters = {};
let answer = '';
let occurance = 1; // TODO find a way to find out occurance
for (let letter of s) {
characters[letter] = (characters[letter] || 0) + 1;
}
let values = Object.values(characters);
let counter = 0;
for (let i = 0; i < values.length; i++) {
if (values[i] !== occurance) {
counter++;
if ((values[i] - 1) !== occurance) {
counter++;
}
}
}
if (counter > 1) {
answer = 'NO';
} else {
answer = 'YES';
};
return [answer, counter];
}
// console.log(isValid('abc'));
// console.log(isValid('abcc'));
// console.log(isValid('abbcc'));
// console.log(isValid('abccc'));
// greedy algorithms
// min absolute difff
function minimumAbsoluteDifference(arr) {
// let diff = 100000000000;
// for (let i = 0; i < arr.length; i++) {
// for (let j = 0; j < arr.length; j++) {
// if (Math.abs(arr[i] - arr[j]) < diff && i!==j) {
// diff = Math.abs(arr[i] - arr[j]);
// }
// }
// }
// return diff
arr.sort((a, b) => a - b);
let min = 0;
for (let i = 0; i < arr.length; i++) {
let absDiff = Math.abs(arr[i+1] - arr[i]);
if (!min || absDiff < min) min = absDiff;
if (min ===0) return 0;
}
return min;
}
console.log(minimumAbsoluteDifference([1, 2, 4, 8]));