Junior Software Dev vs Senior Dev solving Valid Anagram

preview_player
Показать описание
FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

This is less rookie vs. senior and more unprepared rookie vs. prepared rookie.

alxjones
Автор

problems with strings such as this one that solved by hash maps could usually be optimised further by counting in array instead of hash map (lower constant in O(1)). Hint is "ascii only characters". This is how you get to top 5% in both speed and memory

So iterate s, incrementing stats[s_char], then iterate t, decrementing. Return true of stats has all zeroes

Taurdil
Автор

In practical code, sorting an array can be faster and more memory efficient than creating objects and hashing if we don't expect huge n's. In many cases, we don't.

Basta
Автор

The point of leetcode is to try the brute force way first then learn the optimised method and apply that to new problems.

Leafycoke
Автор

Oh anagrams are words using the same letters?

hlubradio
Автор

I could easily solve this in C, but not in Python.

In C, I'd just make an int array of size 26, iterate through the string while doing +/- 1 on the corresponding array elements, and check if the array has a non-zero.

Easiest O(n) ever.

astral
Автор

I'd rather go forward through one and backward through another instead of using the hashmap, more clear, more fun, less space, and makes more sense to anyone who sees it

nk
Автор

Senior devs are actually solving real problems, and are usually annoyed when they have to solve problems like this

MrBranh
Автор

This would require a space of o(m+n) which can be reduced to o(n) by only storing count of s in hashmap and decrementing count while traversing t. If -1 os even reached, return false.

lOmaine
Автор

from experience, this comes up like never and most stuff thats breaking or causing issues is the architecture and/or used frameworks and/or libraries

ZeruelB
Автор

Still a bad solution.
take an integer array of size 26.
increment (s1.charAt(i) - 'a') in a loop.
Then decrement (s2.charAt(i) - 'a') in a loop.
Check if the array has any non-zero value.

The complexity is O(s1Lenth + s2Length + 26) = O(n)

ShinAkuma
Автор

I wonder if you used bit packing or flags on the most common letters to build a checksum value. Then compare on both to break early for 99.99% of cases. The. For the remaining do any slower verification, doesnt matter for performance

HhddGufif
Автор

"Sort is n log n!"
Dude, you're sorting about 10 items, so the O of the algorithm is irrelevant.

bart
Автор

Lol, coding is a really cool thing for me. I love maths, but not only to have to find a solution, but also optimize it is really challenging

hmkl
Автор

😂

const solution = (s, t) => Array.from(t).every(el => Set(s).has(el))

stupid solution but correct

Nellak
Автор

Lmao, if you wanted to over-optimize your code like that:
1. You wouldn't use hash tables. A single array indexed from 0 to 25 (or from 0 to 255 if we allow all ASCII characters) is faster.
2. You wouldn't use Python, lol.

numbdigger
Автор

O(n) with space complexity of O(n) as well
Or
O(nlogn) with space complexity of O(n)

Make sure to tell both approaches the interviewer

PS- Since python doesn't let you sort string in-place both approaches are O(n) space for python developers

rishujeetrai
Автор

Me: Eh, convert both to lists, go over the first list and if you don't find the letter from the first list in the second, they aren't anagrams; if you find it, remove it. It's O(n^2), but hey, that's still gonna be fast for most reasonable inputs... if not, you can always use 2 arrays for the numbers of occurances of each letter and compare them, I guess...

thegoodhen
Автор

The expert dev is the one that realises that your prod code probably wouldn't notice the difference between the two solutions but broke because someone asked if 🤢💛🍇🍊🍌🍍🍎 is an anagram of 🤢🍎💛🍇🍊🍌🍍

ungrim