Switch the vowels in the same word - javascript

How can i switch the vowels in a string to where the first vowel is switch with the last vowel and the second vowel is switched with the second to last vowel and so on and so forth? I've tried to split the input string and attempt an embedded for loop, but I can't seem to figure out where to go from there.
function reverseVowels(input) {
let vowels = ['a','e','i','o','u']
let newArr = input.split('')
let result = [];
for (i = 0; i < newArr.length; i++){
for (j = 0; j < newArr.length; j++)
if (newArr[i] === vowels[i] && newArr[j] === vowels[i]) {
newArr[i] = newArr[j]
}
}
return result;
}
Thank you in advance

Get all the vowels of the input and then reverse it. At last, replace the original arrays vowel with the filtered one.
function reverseVowels(input) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
const a = input
.split('')
.filter((x) => vowels.includes(x))
.reverse();
let count = 0;
return [...input].map((x) => (vowels.includes(x) ? a[count++] : x)).join('');
}
console.log(reverseVowels('ahije'));
console.log(reverseVowels('axeyuzi'));

The most simple solution I can think of is to do a sort of in place reversal where you skip any characters that are not vowels:
function reverseVowels(input) {
const vowels = new Set(['a','e','i','o','u']);
const result = input.split('')
const vowelIndeces = result.reduce((acc, char, i) => {
if (vowels.has(char)) {
acc.push(i);
}
return acc;
}, []);
let i = 0;
let j = vowelIndeces.length - 1;
while (j > i) {
const ref = result[vowelIndeces[i]];
result[vowelIndeces[i]] = result[vowelIndeces[j]];
result[vowelIndeces[j]] = ref;
j--;
i++;
}
return result;
}

I would have a runner start from each end. So left would start at index 0 and increase until it hit a vowel (or end of the string, but I'll let you handle edge cases). Once that happens, use a runner right to start at the end and decrease until it hits a vowel. Swap them and repeat until the pointers cross.

Related

JavaScript How to Create a Function that returns a string with number of times a characters shows up in a string

I am trying to figure out how to make a function that takes a string. Then it needs to return a string with each letter that appears in the function along with the number of times it appears in the string. For instance "eggs" should return e1g2s1.
function charRepString(word) {
var array = [];
var strCount = '';
var countArr = [];
// Need an Array with all the characters that appear in the String
for (var i = 0; i < word.length; i++) {
if (array.indexOf(word[i]) === false) {
array.push(word[i]);
}
}
// Need to iterate through the word and compare it with each char in the Array with characters and save the count of each char.
for (var j = 0; j < word.length; i++) {
for (var k = 0; k < array.length; k++){
var count = 0;
if (word[i] === array[k]){
count++;
}
countArr.push(count);
}
// Then I need to put the arrays into a string with each character before the number of times its repeated.
return strCount;
}
console.log(charRepString("taco")); //t1a1co1
console.log(charRepString("egg")); //e1g2
let str = prompt('type a string ') || 'taco'
function getcount(str) {
str = str.split('')
let obj = {}
for (i in str) {
let char = str[i]
let keys = Object.getOwnPropertyNames(obj)
if (keys.includes(char)) {
obj[char] += 1
} else {
obj[char] = 1
}
}
let result = ''
Object.getOwnPropertyNames(obj).forEach((prop) => {
result += prop + obj[prop]
})
return result
}
console.log(getcount(str))
If the order of the alphanumeric symbols matters
const str = "10zza";
const counted = [...[...str].reduce((m, s) => (
m.set(s, (m.get(s) || 0) + 1), m
), new Map())].flat().join("");
console.log(counted); // "1101z2a1"
Or also like (as suggested by Bravo):
const str = "10zza";
const counted = [...new Set([...str])].map((s) =>
`${s}${str.split(s).length-1}`
).join("");
console.log(counted); // "1101z2a1"
A more clear and verbose solution-
Let m be max number of symbols in charset
Time complexity- O(n log(m))
Space complexity- O(m)
function countFrequencies(str) {
const freqs = new Map()
for (const char of str) {
const prevFreq = freqs.get(char) || 0
freqs.set(char, prevFreq + 1)
}
return freqs
}
function getCountStr(str) {
const freqs = countFrequencies(str)
const isListed = new Set()
const resultArray = []
for (const char of str) {
if (isListed.has(char)) continue
resultArray.push(char)
resultArray.push(freqs.get(char))
isListed.add(char)
}
return resultArray.join("")
}
console.log(getCountStr("egg"))
console.log(getCountStr("taco"))
console.log(getCountStr("10za"))
Using Set constructor, first we will get the unique data.
function myfun(str){
let createSet = new Set(str);
let newArr = [...createSet].map(function(elem){
return `${elem}${str.split(elem).length-1}`
});
let newStr = newArr.join('');
console.log(newStr);
}
myfun('array');

how to return same letters elements with O(n) time complexity in javascript?

I am trying to solve a problem with lower time complexity which in this case O(n).
Here is the problem:
let arr = ['dog', 'come', 'ogd', 'something', 'emoc'];
It should return
[['dog', 'ogd], ['come', 'emoc']]; // which same using letters
So far I solved this problem using two functions it is working great but mine is nested loop will give me O(n2)
Here is my code
const isSameChars = (str1, str2) => {
let str1sorted = str1.split("").sort().join("");
let str2sorted = str2.split("").sort().join("");
if (str1.length !== str2.length) {
return false;
}
for (var i = 0; i < str1sorted.length; i++) {
let char1 = str1sorted[i];
let char2 = str2sorted[i];
if (char1 !== char2) {
return false;
}
}
return true;
}
const isSameCharElements = (arr) => {
let result = [];
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < i; j++) {
if (isSameChars(arr[i], arr[j]) === true) {
result.push(arr[j])
}
}
}
return result;
}
console.log(isSameCharElements(['dog', 'come', 'ogd', 'something',
'emoc'])) // [['dog', 'ogd], ['come', 'emoc']]
Is there any way to solve this with O(n) time complexity?
Thank you an advance!
You can have a 'bag of letters' representation of any String by sorting the letters:
function sortLetters(word){
return word.split('').sort().join('');
}
You can then iterate over your input, grouping words that have the same bag of letters representation into an object:
const grouped = arr.reduce(function (m, word) {
var bagRepr = sortLetters(word);
var withSameLetters = m[bagRepr] || [];
withSameLetters.push(word);
m[bagRepr] = withSameLetters;
return m;
}, {});
const result = Object.values(grouped)
.filter(function (arr) {
return arr.length > 1;
});
This is O(n), provided that sortLetters() is O(1), which is the case if the words lengths are bounded by a constant.
Disclaimer: note that we're only talking about asymptotic complexity here - this does not mean at all that this approach is the most efficient from a practical standpoint!

JavaScript - Find a word string out of strings in a two dimensional array

I am trying to find a word from consecutive strings inside a two dimensional array.
For example:
array = [[0,'r',0,0,0,0,0],
[0,'a',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'e',0,0,0,0,0]];
I want to make a function that will return true if the word 'apple' is inside this array vertically. Strings need to be consecutive.
Or:
array1 = [[0,'e',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'a',0,0,0,0,0],
[0,'q',0,0,0,0,0]];
It should work from top to bottom and from bottom to top.
This should return false since there are no consecutive letters:
array2 = [[0,'e',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'a',0,0,0,0,0],
[0,'q',0,0,0,0,0]];
Can you help please?
Here's a function that does exactly what you need:
let array1 = [
[0,'r',0,0,0,0,0],
[0,'a',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'e',0,0,0,0,0]
];
let array2 = [
[0,'r',0,0,0,0,0],
[0,'e',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'a',0,0,0,0,0]
];
function includesWordVertically(matrix, word) {
for (let j = 0 ; j < matrix[0].length ; j++) {
let verticalWord = '';
for (let i = 0 ; i < matrix.length ; i++) {
verticalWord += matrix[i][j];
}
if ((verticalWord.includes(word)) ||
(verticalWord.split('').reverse().join('').includes(word)))
{
return true;
}
}
return false;
}
console.log(includesWordVertically(array1, 'apple'));
// true
console.log(includesWordVertically(array2, 'apple'));
// true
Note that this function does not do the necessary checks (e.g. matrix not empty, all rows have the same length, etc).
I would combine a single string from all characters in one vertical column, and also add another set of same characters, so if the word Apple is divided, you will fins it is a string. After adding all characters twice, you will get a string like 'leappleapp' and you will find an apple there
Returns true Only if found in a straight column.
var array1 = [[0,'a',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'p',0,0,0,0,0],
[0,'l',0,0,0,0,0],
[0,'e',0,0,0,0,0],
[0,'q',0,0,0,0,0]];
function isVertically(array, word) {
var string = "";
var index = -1;
for(var i = 0; i < array.length; i++) {
var line = array[i];
for(var j = 0; j < array.length; j++) {
var element = line[j];
if(typeof element == "string") {
if(index < 0)
index = j;
if(j === index)
string += element;
}
}
}
return string == word;
}
isVertically(array1, "apple")

replace all vowels in a string javascript

I am trying to write a function that will remove all vowels in a given string in JS. I understand that I can just write string.replace(/[aeiou]/gi,"") but I am trying to complete it a different way...this is what I have so far... thank you!
I first made a different function called IsaVowel that will return the character if it is a vowel...
function withoutVowels(string) {
var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
if (isaVowel(string[i])) {
***not sure what to put here to remove vowels***
}
}
return withoutVowels;
}
Use accumulator pattern.
function withoutVowels(string) {
var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
if (!isVowel(string[i])) {
withoutVowels += string[i];
}
}
return withoutVowels;
}
function isVowel(char) {
return 'aeiou'.includes(char);
}
console.log(withoutVowels('Hello World!'));
I tried doing this problem by first splitting the string into an array, while also creating an array of vowels. Then go through each element in the string array and check whether it's in my vowel array. If it is not in my vowel array, push it to the withoutVowels array. At the end of the for loop, join all elements in the withoutvowels array and return.
function withoutVowels(string) {
var strWithoutVowels = [];
string = string.split('');
var vowels = ['a', 'e', 'i', 'o', 'u'];
for (var i = 0; i < string.length; i++) {
if (vowels.indexOf(string[i]) < 0) {
strWithoutVowels.push(string[i])
}
}
strWithoutVowels = strWithoutVowels.join('');
return strWithoutVowels;
}
console.log(withoutVowels('Hello World!'))
I think the easiest way is to use a regex; it's cleaner and faster compared to all your loops. Below is the code.
string.replace(/[aeiou]/gi, '');
the gi in the code means no matter the case whether uppercase or lowercase so long as its a vowel, it will be removed

Capture non-adjacent repeating letters

How do I capture repeating letters in a word like abababa = 2matches( a and b is repeating )
I know how to do it when the letters are adjacent like so /(\w)\1+/ .
Thanks
Try to use this String expansion:
String.prototype.getRepeating = function() {
var length = this.length;
var found = '';
var repeating = '';
var index;
var letter;
for (index = 0; index < length; index++) {
letter = this.charAt(index);
if (-1 == found.indexOf(letter)) {
found = found.concat(letter);
} else {
if (-1 == repeating.indexOf(letter)) {
repeating = repeating.concat(letter);
}
}
}
return repeating;
}
The tests:
var tests = ['ab', 'aa', 'bb', 'abab', 'abb', 'aab', 'bab'];
for (var index in tests) {
console.log(tests[index], '=>', tests[index].getRepeating());
}
ab => (an empty string)
aa => a
bb => b
abab => ab
abb => b
aab => a
bab => b
If I understand correctly, you want to extract letters which appear more than once in a given word. If so, you simply need to iterate over the letters of the word, accumulate their occurrence, then filter out letters which only appear once.
var testString = "abababa";
var letters = countGroupByLetter(testString);
var result = filterMap(letters, function(v) {
return v > 1;
});
console.log(result);
function countGroupByLetter(testString) {
var result = {};
for (var ii = 0; ii < testString.length; ii++) {
var letter = testString.charAt(ii);
if (result[letter]) {
result[letter] ++;
} else {
result[letter] = 1;
}
}
return result;
}
function filterMap(map, filterFunction) {
var result = {};
for (var p in map) {
if (filterFunction(map[p])) {
result[p] = map[p];
}
}
return result;
}
Since you already know about back references, I suppose that you know you can find out if there is a letter repetition in a string, using /(\w).*\1/. Capturing all repetitions in one pass would not be possible though, you'd still need to execute a pattern repeatedly and accumulate the matched characters (for instance using /(\w)(?=.*\1)/g). That, however, would not be optimal.
var repeatingLetters = /(\w)(?=.*\1)/g;
var testString = "abababa";
var captures = null;
var result = {};
while ((captures = repeatingLetters.exec(testString)) != null) {
result[captures[1]] = true;
}
console.log(result);

Categories