Reputation: 38
Suppose that i have a word "FadTheChad" (or any word that has vowels) and want the vowels replaced with "*" and return all the possibilities (subsets? not sure how i could describe it) of the word in an array.
I tried using String.replace() on the whole string which gave me F*dTh*Ch*d
. Then i tried looping over the string and doing .replace() again which returned F*dTheChad, FadTh*Chad, FadTheCh*d
.
is there anyway that i could get something like
[
'F*dTh*Ch*d',
'F*dTheChad',
'F*dTh*Chad',
'F*dTheCh*d',
'FadTh*Chad',
'FadTh*Ch*d',
'FadTheCh*d'
]
Upvotes: 1
Views: 836
Reputation: 11
replace Vowel world in string using "e"
//let vowels = /[aeiou]/gi;
//let result = str.match(vowels);
//return result;
let newstr = str.replace(/[aeiou]/gi,'e')
return newstr.toUpperCase();
}
//toScottishScreaming();
console.log(toScottishScreaming("ewefpofdfdfkfg hythythg fgrefek!"));
Upvotes: 0
Reputation: 50797
Ok, now that I understand the question before answering it (see original below for explanation), this can also be answered with a simple recursion:
const addAsterisks = ([c,...cs]) =>
c == undefined
? ['']
: addAsterisks (cs) .flatMap (
(w) => 'aeiou' .includes (c) ? [c + w, '*' + w] : [c + w]
)
console .log (addAsterisks ('FadTheChad'))
.as-console-wrapper {max-height: 100% !important; top: 0}
If the string is empty, we return an array containing the empty string. Otherwise we recur on the remainder of the string, and for each result, we return an array with the our current character prepended to the result and, if the current character is a vowel, an asterisk prepended to the result.
(This answered a different question altogether, based on my misreading. It's still interesting in its own right.)
A simple recursion makes this easy:
const addVowels = (str) =>
str .indexOf ('*') < 0
? str
: ['a', 'e', 'i', 'o', 'u'] .flatMap (v => addVowels (str .replace ('*', v)))
console .log (addVowels ('F*dTh*Ch*d'))
.as-console-wrapper {max-height: 100% !important; top: 0}
If our string contains no asterisks, we are done and simply return it.
If it has an asterisk, flatMap
ping the results of replacing that with each of the vowels and recurring on the resulting string.
For next time, please include some sample code for what you've tried.
Upvotes: 1
Reputation: 28196
I am way too late, but maybe this is still of interest so someone?
const str="didadumdidum",v=[],
parts=str.replace(/[aeiou]/gi,
m=>(v.push(m),"*")).split("*"),
n=1<<v.length;
for (let i=0; i<n; i++) {
let s=i.toString(2)
.padStart(v.length,"0")
console.log(
parts.map((p,j)=>(j--?+s[j]?v[j]:"*":"")+p).join("")
)
}
In my approach I used String.prototype.replace()
with a callback function to collect the vowels in an array while splitting the string into an array (parts
). The rest is similar to @thebombsquad's approach.
Upvotes: 0
Reputation: 4337
So the question is to find the vowels, then to find all possible combinations by replacing them with star.. pretty nice so here's my attempt
function vowelOptions(text,replacer){
let isVowel={a:1,e:1,i:1,o:1,u:1}
let vowels=[], toReturn=[]
let data=text.split('')
data.forEach((letter,i)=>{
if(isVowel[letter]){vowels.push(i)} //location of vowel stored
})
for(let i=1;i<Math.pow(2,vowels.length);i++){
//Math.pow(2,vowels.length) are the number of vowel possibilities
//i=1 and NOT 0 because default text is now allowed in return
let dataOption=[...data] //a clone with a possible combination
let combination=i.toString(2).split('').reverse() //the ones that will be '1' tell me where to put the replacer
combination.forEach((bool,j)=>{
if(bool=='1'){dataOption[vowels[j]]=replacer} //vowels to replace in this combination replaced thanks to vowel locations saved above
})
toReturn.push(dataOption.join(''))
}
return toReturn
}
//now watch it work >:}
console.log(vowelOptions("FadTheChad","*"))
Upvotes: 1
Reputation: 181
Try this:
function getPossibilities(word) {
let vowels = ['a', 'e', 'i', 'o', 'u']; // 'y'?
let positions = [];
for (let i = 0; i < word.length; i++) {
if (vowels.indexOf(word[i]) !== -1) positions.push(i);
}
let result = [], count = 2 ** positions.length;
for (let i = 0; i < count; i++) {
let newWord = [...word];
for (let j = 0; j < positions.length; j++) {
if ((i >> j) & 1) newWord[positions[j]] = '*';
}
result.push(newWord.join(''));
}
return result;
}
I make the word into an array because in JS strings are immutable so I wouldn't be able to modify indices like this.
Upvotes: 2