Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Văn Trần Phú Quí
Simpler solution with JS

Văn Trần Phú Quí

Nov 26, 2024

I found this solution is simpler than posted solution. Just 1 question, do we need to check if length of s and t is equal to other?

class Solution {   isAnagram(s, t) {     if(s.length !== t.length) return false;     for(let i = 0; i < s.length; i++){       if(!t.includes(s[i])){         return false;       }     }     return true;   } };

0

0

Comments
Comments
Văn Trần Phú Quí
Văn Trần Phú Quía year ago

I just tried to solve this solution on Leetcode, but it didn't pass all test cases.

So I changed to use the solution below instead

function isAnagram(s: string, t: string): boolean { const map = new Map<string, number>(); if (s.length !== t....
Design Gurus
Design Gurus10 months ago

This is a O(n^2) algorithm; because of the 'for' loop and 't.includes(...)'.

Our solution is O(n) time and O(n) space, which is generally considered better.

Vishnu S Nair
Vishnu S Nair8 months ago

This is incorrect. The function only checks whether all characters in s exist in t, but it does not check if they appear the same number of times.