Skip to content

anagram algorithm #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 9. Anagram/anagramChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function anagramChecker(a, b) {
return a.toLowerCase().split('').sort().join('') === b.toLowerCase().split('').sort().join('');
}

anagramChecker("Listen", "Silent");
118 changes: 76 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Algorithmic_javascript
<br>

> ## This is open-source Project and Part HACKTOBERFEST 2020
> ## This is open-source Project and Part HACKTOBERFEST 2020

#### <font color='green'> Contribute to open source and make the world *A Better Place* </font>

Expand Down Expand Up @@ -34,9 +34,9 @@

#### Rules

* To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone).
* To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone).
* PRs can be made to any public repo on GitHub, not only the ones with issues labeled Hacktoberfest. If a maintainer reports your pull request as spam or behavior not in line with the project’s code of conduct, you will be ineligible to participate.
* This year, the first 70,000 participants who successfully complete the challenge will be eligible to receive a prize.
* This year, the first 70,000 participants who successfully complete the challenge will be eligible to receive a prize.

<br>

Expand Down Expand Up @@ -64,23 +64,24 @@ __If you want to contribute then follow theses steps__
> 5.1 A short Introduction <br>
> 5.2 The challenge<br>
> 5.3 Algorithmic thinking<br>
> 5.4 Code Implementation <br>
> 5.4 Code Implementation <br>
6. add two hr tags after each problem in README.md file <br>

### Star the repository If you enjoyed contributing to open source projects.
<br>

__Algorithms practiced using JS__

## List of problems
1. String reversing
2. Vowel counter
2. Vowel counter
3. Finding the Most Recurring Character
4. Sentence Capitalization
5. Palindromes
6. Pig Latin
7. Deep Compare
8. Binary Search Tree
9. Anagrams

## Explanation
<b>1. String reversing</b>
Expand Down Expand Up @@ -112,7 +113,7 @@ We will now explore ways to solve this challenge below. They are:

*1.Chaining in-built methods:*


The **.split()** method is used to split a string into an array of characters. It receives one argument which specifies the separator that determines where every split occurs.


Expand Down Expand Up @@ -164,7 +165,7 @@ __Algorithmic Thinking:__ <p> After reading the problem statement, __ given text

> A function is a block of organized, reusable code that is used to perform a single, related action. They may or may not accepts value as parameter for computational tasks. The values are also called as Arguments.

*Let's Breakdown the approach*
*Let's Breakdown the approach*

* write a function which receives a parameter called "text". It should be string of any desired length which is to be passed in function as an argument.

Expand All @@ -175,7 +176,7 @@ __Algorithmic Thinking:__ <p> After reading the problem statement, __ given text
* Function will return the number of vowels found. So you have to use __*return*__ function which stops the the function execution and returns a value.
<br>

__Code Implementation:__
__Code Implementation:__
<p> We are going to use Two approaches for solving this problem:</p>

1. Using Iterative approach
Expand Down Expand Up @@ -206,7 +207,7 @@ console.log(vowelcnt("i love HacktoberFest"))

**Breaking-down the steps:**

* Firstly, we declard const vowel which is array of five vowels.
* Firstly, we declard const vowel which is array of five vowels.
* We declare a function and initialize the counter.
* We make use of For loop to itearte over the given string. Next, we convert the all letters of string to lowercase as we don't want to miss out on uppercase letters.
* In for loop,use if to check if selected letter is included in the array of vowels which we declared earlier. We call *includes()* method on array of vowels to check whether the array includes selected letter or not.
Expand All @@ -229,7 +230,7 @@ __The challenge:__ Given a string of text, find and return the most recurring ch

```js
maxRecurringChar('aabacada') // will return 'a'
```
```

__Algorithmic Thinking:__ <p> From the challenge statement, we can infer that our function has only one parameter; the string of text.<br> We need to somehow keep track of every character in the string as well as the number of times it exists. <br> This we would describe as character mapping. Once we successfully create such a map, we can easily determine which character has the highest occurence. </p>
<br>
Expand Down Expand Up @@ -261,7 +262,7 @@ for example: In string "success" <br>

```js

/*
/*
maxCharValue is used to store the maximum value yet encountered at the point of every iteration with the for---in loop.

maxChar is used to store the character with the highest value on every iteration.
Expand Down Expand Up @@ -302,8 +303,8 @@ console.log(maxRecurringChar('success'))
<p> Often during presentation situation arises where we need to manipulate the letter casing of strings within our application. Javascript offers two popular methods designed for this purpose:

1.toUpperCase(): this method returns the string passed in as an argument converted in uppercase letters. <br>
2.toLowerCase(): this method returns the string passed in as an argument converted to lowercase letters.

2.toLowerCase(): this method returns the string passed in as an argument converted to lowercase letters.

__The challenge:__ Given a sentence containing two or more words, return the equivalent of the sentence when capitalized. E.g
```js
Expand All @@ -314,7 +315,7 @@ capitalSentence("a cat and a dog") // would return "A Cat And A Dog"
__Algorithmic Thinking:__ <p>
At a glance this may seem like a very simple challenge, but it actually isn’t when considered carefully.

Working through the challenge, it seems that we need to write a function that'd receive the sentence to be converted as an argument
Working through the challenge, it seems that we need to write a function that'd receive the sentence to be converted as an argument
Next,we go through every word in sentence and capitilize every first letter of word. This brings concept of LOOP to mind
</p>

Expand All @@ -335,19 +336,19 @@ __Code Implementation:__
});

return capsarray.join(' ')

}
console.log(sentenceCap("ARTIFICIAL"))
console.log(sentenceCap("ARTIFICIAL"))
//will return Artificial
```

<p>
<p>

* We call the .toLowerCase() method on the string of text received to convert the entire sentence to lowercase. We also chain the .split() method in sequence to divide the lowercase sentence into an array of words as shown below. This array is stored as wordsArray
</p>
<p>

* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
* The function takes the first letter of the word and turns it to uppercase using the .toUpperCase() method. To retrieve the remaining part of the word in lowercase, we use the .slice() method to slice the string starting from position 1 till the end.

* We combine the transformed first letter and the sliced section together to form the capitalized word which we push into our array of capitalized words capsArray.
Expand Down Expand Up @@ -388,7 +389,7 @@ __Code Implementation:__


<b>5. Palindromes </b>

*What is a Palindrome:* A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar". <br>

__The challenge:__ <p>Given a string of text, return true or false indicating whether or not the text is a palindrome. e.g </p>
Expand All @@ -414,7 +415,7 @@ __code Implementation:__ <p> In this challenge, we'd consider two, yet three wa

1. An Intuitive Approach:
```js

function palindromeCheck(text){
var reverseText= text.toLowercase().split(' ').reverse().join(' ' )

Expand All @@ -424,7 +425,7 @@ __code Implementation:__ <p> In this challenge, we'd consider two, yet three wa
Let's unveil the "mysteries":

* Firstly, the function accepts the string that is to be tested
* Next, all the letters are converted to lowercase,then <font color="red" >.split()</font> method is called on string that is received as text
* Next, all the letters are converted to lowercase,then <font color="red" >.split()</font> method is called on string that is received as text
* Next, we call <font color="red" > .reverse()</font> to re-order the array elements in reverse.

* After that <font color="red" >.join()</font> is called on reversed array to form a whole string.
Expand All @@ -434,7 +435,7 @@ Let's unveil the "mysteries":
2. Looping Through and Comparing Characters:

This could be a bit confusing than the previous implementation.
We will break it into simple steps.Stay in the game.
We will break it into simple steps.Stay in the game.
* For example, If we have to test string "machine", we will compare "m" with "e", because if the string is reversed then "e" will take m's position

* Similarly, "a" will be compared to "n".
Expand All @@ -455,7 +456,7 @@ Let's unveil the "mysteries":
```

Let's review it:
* First we convert the string to lowercase and after it we use <font color="red" >.split()</font> method
* First we convert the string to lowercase and after it we use <font color="red" >.split()</font> method

* We use special array method <font color="red" >.every()</font> to loop through array & perform our check. In fact,<font color="red" >.every()</font>
method tests whether all elements pass the test or not which is implemented by provided function
Expand Down Expand Up @@ -509,7 +510,7 @@ On the other hand, a declarative approach would abstract this process, allowing

[Source](https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/pig-latin)

__code Implementation:__
__code Implementation:__

1. Imperative Approach

Expand Down Expand Up @@ -603,45 +604,45 @@ Note that we chain both .replace() methods in succession such that both cases ar

Comparing objects can be troublesome, not to mention multi-dimensional objects/arrays. Here is something simple to help.

__The challenge:__ <p> - JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays a and b we cannot just use normal comparison operators.</p>
```js
__The challenge:__ <p> - JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays a and b we cannot just use normal comparison operators.</p>
```js
a === b //false
```
<p> - Use of multidimensional objects/arrays is possible, making it difficult to compare simply by iteration since we don't know the depth of a value. </p>

<p> - Different data types like Dates and undefined must also be taken into consideration. <p>

<p>Given the above, return a boolean signifying whether a and b are equivalent in content. </p>

__Algorithmic Thinking:__ <p>As we would be comparing each item contained in the objects, a loop may be the first instinct to solving it. However, with the potential of multidimensional iterables, we would have to disect nested arrays in the same way when we encounter them. A combination of iteration and recursion is therefore necessary. So for each item of the array a data type check is necessary as well, to allow execution of a relevant comparison.

Breaking it down:
* check if ```a === b```
* check if ```a``` and ```b``` are both iterable
* iterate over ```a``` using keys and call deepCompare recursively
</p>


__code Implementation:__ <p>
__code Implementation:__ <p>

Firstly, we'll do the most simple check of ```a === b``` to avoid unnecessary complexity. This will process all of the equal literal values for us.

```js
```js
if(a === b) return true
```

Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and b are of type object. If not, we can just return false as they didn't pass the ```a === b``` test.

```js
```js
if(typeof a === "object" && typeof b === "object")...
```

Note that we use ```===``` here to differentiate between data types strictly.

Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead of the date object itself.

```js
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
```js
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
```

Lastly, by taking the keys of the iterables we can compare the length of ```a``` and ```b```, then make use of built-in Array.some method to check if any values of the two iterables don't match.
Expand All @@ -652,10 +653,10 @@ const keysA = Object.keys(a)

//make sure a and b are the same length
if(keysA.length !== Object.keys(b).length) return false

//Array.some() iterates through the values in an array and stops executing nested code until there is one that returns true
//in this case that would be when there is one different value between a and b
return !keysA.some( key => {
return !keysA.some( key => {
//run deepCompare recursively
return !deepCompare(a[key], b[key])
})
Expand All @@ -671,7 +672,7 @@ const deepCompare = (a, b) => {
if(typeof a === "object" && typeof b === "object")
{
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
else
else
{
const keysA = Object.keys(a)
if(keysA.length !== Object.keys(b).length) return false
Expand All @@ -683,7 +684,7 @@ const deepCompare = (a, b) => {
else return false
}

deepCompare(1, 2)
deepCompare(1, 2)
//false

deepCompare({"first": 1, "second": 2}, {"first": 1, "second": 2})
Expand All @@ -693,16 +694,17 @@ deepCompare([1, "2", 3.0], [1, "2", 3])
//false

const arr = [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]
deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]])
deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]])
//true

```

It's that simple! Hope this helps.
</p>
<hr>
<hr>


<b>8. Binary Search Tree </b>
Building, traversing or finding values in Binary Search Trees

Expand Down Expand Up @@ -971,7 +973,39 @@ It's very simple ones you realize it.
<hr>
<hr>

<b>10. Name </b>
<b>10. Anagram</b>

An anagram is a word formed by rearranging the letters of a different word, using all the original letters exactly once.

__The challenge:__ <p>Given 2 strings of text, write an algorithm that return true or false indication wheter or not those string are anagrams. eg.</p>
```js
anagramChecker("Listen", "Silent");
```

__Algorithmic Thinking:__ <p>Our function must receive 2 parameters, as we need to compare 2 strings.</p>
<p>Function should check if words are made of the same letters. Most straightforward approach seems to be something like in the most recurring character problem, but it will lead to long and complicated code.</p>
<p>Much simpler code could be written using build in JS functions when we realize that characters order in words does not matter (as stated in anagram definition), so if we sort them in boths strings and compare - we will know if they are anagrams or not.</p>

__code Implementation:__ <p>To use build in `sort` method on string we must change it into array, which can be done using `split` method with `''` as argument.</p>
```js
string.split('').sort()
```
<p>Then we must change this new array again into string, using `join`</p>
```js
string.split('').sort().join()
```
<p>We should also notice that strings should be compared case insensitive, so we need to 'normalize' their case</p>
```js
string.toLowerCase().split('').sort().join()
```
<p>When we do those thing for both strings - we can check if they are eqaual - which will tell us if they are anagrams:</p>
```js
function anagramChecker(a, b) {
return a.toLowerCase().split('').sort().join('') === b.toLowerCase().split('').sort().join('');
}
```

<b>11. Name </b>

__The challenge:__ <p> </p>

Expand All @@ -983,7 +1017,7 @@ __code Implementation:__ <p> </p>
<hr>
<hr>

<b>11. Name </b>
<b>12. Name </b>

__The challenge:__ <p> </p>

Expand Down