1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add frequency-deviation/javascript/function

This commit is contained in:
Théo LUDWIG 2023-09-16 15:50:52 +02:00
parent 8aaf81bc65
commit d7c5790aba
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# frequency-deviation/javascript/function
Created by [@theoludwig](https://github.com/theoludwig) on 16 September 2023.

View File

@ -0,0 +1,55 @@
/**
*
* @param {string} string
* @returns {number}
*/
export const getMaximumFrequencyDeviation = (string) => {
/** @type {string[]} */
const subStrings = []
for (let index = 0; index < string.length; index++) {
let subString = ''
for (let subIndex = index; subIndex < string.length; subIndex++) {
subString += string[subIndex]
subStrings.push(subString)
}
}
let maximumFrequencyDeviation = 0
for (const subString of subStrings) {
/**
* @type {Map<string, number>}
*
* @description
* - Key: Character in the string
* - Value: Number of occurrences of the character in the string
*/
const map = new Map()
for (let index = 0; index < subString.length; index++) {
const character = subString[index]
if (map.has(character)) {
map.set(character, map.get(character) + 1)
} else {
map.set(character, 1)
}
}
let minimumFrequence = Number.MAX_VALUE
let maximumFrequence = 0
for (const [_, value] of map) {
if (value > maximumFrequence) {
maximumFrequence = value
}
if (value < minimumFrequence) {
minimumFrequence = value
}
}
const frequencyDeviation = maximumFrequence - minimumFrequence
if (frequencyDeviation > maximumFrequencyDeviation) {
maximumFrequencyDeviation = frequencyDeviation
}
}
return maximumFrequencyDeviation
}

View File

@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@ -0,0 +1,18 @@
import readline from 'node:readline'
import { getMaximumFrequencyDeviation } from './getMaximumFrequencyDeviation.js'
const input = []
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
readlineInterface.on('line', (value) => {
input.push(value)
})
const solution = () => {
console.log(getMaximumFrequencyDeviation(input[0]))
}
readlineInterface.on('close', solution)