Skip to content
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

Solution of assignment #1158

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
11 changes: 0 additions & 11 deletions 01-js/easy/anagram.js

This file was deleted.

76 changes: 76 additions & 0 deletions 01-js/easy/isAnagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Write a function `isAnagram` which takes 2 parameters and returns true/false if those are anagrams or not.
What's Anagram?
- A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.
*/



function isAnagram(str1, str2) {
// Step 1: Normalize the strings (convert to lowercase and remove any whitespace)
str1 = str1.replace(/\s+/g, '').toLowerCase();
str2 = str2.replace(/\s+/g, '').toLowerCase();

// Step 2: Check if the lengths are different; if so, they cannot be anagrams
if (str1.length !== str2.length) {
return false;
}

// Step 3: Sort the characters in each string and compare them
const sortedStr1 = str1.split('').sort().join('');
const sortedStr2 = str2.split('').sort().join('');

// Return true if sorted strings are equal, otherwise false
return sortedStr1 === sortedStr2;
}

// Test cases
console.log(isAnagram("spar", "rasp")); // true
console.log(isAnagram("Listen", "Silent")); // true
console.log(isAnagram("hello", "world")); // false
console.log(isAnagram("The Eyes", "They See")); // true
console.log(isAnagram("a gentleman", "elegant man")); // true

module.exports = isAnagram;







// // Alternative Approach: Using Frequency Count (Improves Performance)

// function isAnagram(str1, str2) {
// str1 = str1.replace(/\s+/g, '').toLowerCase();
// str2 = str2.replace(/\s+/g, '').toLowerCase();

// if (str1.length !== str2.length) {
// return false;
// }

// const count = {};

// // Count the frequency of each character in str1
// for (let char of str1) {
// count[char] = (count[char] || 0) + 1;
// }

// // Subtract the frequency using characters in str2
// for (let char of str2) {
// if (!count[char]) {
// return false;
// }
// count[char]--;
// }

// return true;
// }

// console.log(isAnagram("spar", "rasp")); // true
// console.log(isAnagram("Listen", "Silent")); // true
// console.log(isAnagram("hello", "world")); // false
// console.log(isAnagram("The Eyes", "They See")); // true
// console.log(isAnagram("a gentleman", "elegant man")); // true

// module.exports = isAnagram;
4 changes: 4 additions & 0 deletions 01-js/easy/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isAnagram = require('./isAnagram');

console.log(isAnagram("spar", "rasp")); // true
console.log(isAnagram("hello", "world")); // false