Solving Popular Algorithms: Replace “?”s

Cute doggy + ?s
let getRandomChar = (prev, nxt) => {
let alphaArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

let randomChar = alphaArr[Math.floor(Math.random() * alphaArr.length)]
while (randomChar === prev || randomChar === nxt) {
randomChar = alphaArr[Math.floor(Math.random() * alphaArr.length)]
}

return randomChar
}
var modifyString = function(s) {

let splitS = s.split("")

for (let i = 0; i < splitS.length; i++) {
if (splitS[i] === '?' && i > 0) {
let prev = splitS[i-1]
let nxt = splitS[i+1]
let newChar = getRandomChar(prev, nxt)
splitS.splice(i, 1, newChar)
} else if (splitS[i] === '?') {
let nxt = splitS[i+1]
let newChar = getRandomChar("1", nxt)
splitS.splice(i, 1, newChar)
} else if (splitS[i] === '?' && !splitS[i+1]) {
let prev = splitS[i-1]
let newChar = getRandomChar(prev, "1")
splitS.splice(i, 1, newChar)
}
}

return splitS.join("")

};
Cute doggy 2 + ?s

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Final Keyword in Java

Spring Cloud Eureka and Hystrix Circuit Breaker using Microservices

Two Ways to Immediately Improve Your Python Code

Python message

Symbolic Links to public_html

Get bugs fixed faster with the right description

Azure Database for PostgreSQL — Flexible Server and Features

Master .NET Interactive with F#

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HopeGiometti

HopeGiometti

More from Medium

Leetcode — Design Add and Search Words Data Structure

Range Sum Query 2D — Immutable