-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.js
43 lines (40 loc) · 906 Bytes
/
sol.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
function isValid(s) {
if (s.length <= 3) {
return 'YES'
}
const charsCount = {}
s.split('').map(c => {
if (!(c in charsCount)) {
charsCount[c] = 0
}
charsCount[c]++
})
const occurences = {}
Object.values(charsCount).map(oc => {
if (!(oc in occurences)) {
occurences[oc] = 0
}
occurences[oc]++
})
const occurencesCount = Object.keys(occurences).length
if (occurencesCount > 2) {
return 'NO'
}
if (occurencesCount == 1) {
return 'YES'
}
if ('1' in occurences && occurences['1'] == 1) {
return 'YES'
}
const [fkey, skey] = Object.keys(occurences)
if (Math.abs(parseInt(fkey)-parseInt(skey)) == 1) {
const max = Math.max(parseInt(fkey), parseInt(skey))
if (occurences[max+''] == 1) {
return 'YES'
}
}
return 'NO'
}
const res = isValid('aaaabbcc')
console.log(res)
// export default isValid