Algorithm and Data Structure part 2

Anagrams Check if 2 strings are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces, or special characters.

Anagrams

Check if 2 strings are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces, or special characters. Consider capital letters to be the same as lower case.

Way 1 : Basic solution

```function charMapper(str)
{
const charMap={};

for(let item of str.replace(/[^\w]/g,"").toLowerCase())
{
charMap[item]=charMap[item]+1||1;
}

return charMap;

}

function anagrams(stringA, stringB) {

let charA=charMapper(stringA);
let charB=charMapper(stringB);

if(Object.keys(charA).length!== Object.keys(charB).length)
{
return false;
}

for(let char in charA)
{
if(charA[char] !== charB[char])
{
return false;
}
}
return true;

}

console.log(anagrams("Hello", "oellh"));```

way2: using string functions

```function anagrams(stringA, stringB) {

stringA =stringA.replace(/[^\w]/g,"").toLowerCase().split('').sort().join('');
stringB =stringB.replace(/[^\w]/g,"").toLowerCase().split('').sort().join('');

if(stringA === stringB)
{
return true;
}
else
{
return false;
}
}

console.log(anagrams("Hello", "olleh"));```

Capitalization

Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.

Way 1:

```function capitalize(str) {
let words =[];
let strmap = str.split(' ');
for(let item of strmap)
{
words.push(item[0].toUpperCase() + item.substring(1, item.length));
//words.push(item[0].toUpperCase() + item.slice(1));
}

return words.join(' ');
}

console.log(capitalize("a short sentence"));```

Printing Steps

Write a program that takes an interger and console log a step shape with N levels using the #character making sure there are spaces on the right side.

```function steps(n) {

for(let i=0; i<n; i++)
{
let shape="";
for(let j=0; j<n; j++)
{
(j<=i)? (shape =shape+"#"):(shape =shape +" ");
}
console.log(shape);
}
}

Steps(5)```

Pyramid Problem

Write a function that accepts a positive number N.The function should console log a pyramid shapewith N levels using the # character. Make sure the pyramid has spaces on both the left and right hand sides.

```function pyramid(n) {

const midpoint =Math.floor((2*n-1)/2);
for(let row =0; row<n; row++)
{
let level='';

for(let col=0; col<(2*n-1); col++)
{
if(midpoint -row<=col && midpoint + row >=col)
{
level +='#';
}
else
{
level+=' ';
}
}
console.log(level);
}

}

pyramid(5);```

Find The Vowels

Way 1: simple way

```function vowels(str) {
let count=0;
let charmap = str.toLowerCase().split('');

for(var char of charmap)
{
if(char ==="a" || char ==='e' ||char ==='i' || char ==='o' || char ==='u')
{
count +=1;
}
}
return count;
}

Way 2: using string helper classes

```function vowels(str) {
let count=0;
const checkvowel = ['a','e','i','o','u'];
//const checkvowel ="aeiou";

let charmap = str.toLowerCase().split('');

for(var char of charmap)
{
if(checkvowel.includes(char))
{
count +=1;
}
}
return count;
}

Matrix Spiral

Write a program such that interger N is accepted and it returns a NxN matrix.

// matrix(4)
// [[1, 2, 3, 4],
// [12, 13, 14, 5],
// [11, 16, 15, 6],
// [10, 9, 8, 7]]

```function matrix(n) {

//direction variables
let startRow = 0;
let startColumn = 0;
let EndRow = n;
let EndColumn = n;
let arr = [];
let val = 1;

for(let i = 0; i< n; i++)
{
arr[i] = [];
}

while(startRow<EndRow && startColumn<EndColumn)
{
for(let j=startColumn; j<EndColumn; j++)
{
arr[startRow][j]= val++;
}
startRow +=1;

for(let j=startRow; j<EndRow; j++)
{
arr[j][EndColumn-1]= val++;
}
EndColumn -=1;

for(let j=EndColumn-1; j>=startColumn; j--)
{

arr[EndRow-1][j]= val++;
}
EndRow -=1;

for(let j=EndRow-1; j>=startRow; j--)
{
arr[j][startColumn]= val++;
}
startColumn+=1;

}

return arr;
}

console.log(matrix(5));```

way2:

```function matrix(n) {
const results = [];

let counter = 1;
let startColumn = 0;
let endColumn = n - 1;
let startRow = 0;
let endRow = n - 1;

for(let i = 0; i < n; i++){
results.push([]);
}

while(startColumn <= endColumn && startRow <= endRow){
//Top Row
for(let i = startColumn; i <= endColumn; i++){
results[startRow][i] = counter;
counter++
}
startRow++;

//Right column
for(let i = startRow; i <= endRow; i++){
results[i][endColumn] = counter;
counter++;
}
endColumn--;

//Bottom row
for(let i = endColumn; i >= startColumn; i--){
results[endRow][i] = counter;
counter++;
}
endRow--;

//Start column
for(let i = endRow; i >= startRow; i--){
results[i][startColumn] = counter;
counter++
}
startColumn++;
}
return results;
}```