Meta Coding Interview Question - Letter Combinations of a Phone Number - Leetcode 17

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

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

GregHogg
Автор

If anyone learned discrete math it's combinatorial

hlubradio
Автор

Calculate permutations of the letters P((a, b, c), (d, e, f)) then check each result.

AceReslove
Автор

Use a for loop to iterate over the lenght of the number ( be it 23, 345, etc ) and then use two for loops for the actual char maps for each digit

christianrazvan
Автор

A bfs level by level would also work, you would stop when you got to the level where you have completed all the numbers instead of when the queue is empty. Basically, every time you pull from the queue, add the letters from this level on that, and put it back into the queue.

Basta
Автор

If you press "23" you're gonna get "AD", simple

PFnove
Автор

If you have only 2 numbers max as input, then the o(0) is to have the map calculated once with all combinations. Seems large but in reality its not that much. If its N amount of numbers for the input then this would be better.

MatiasBenavides
Автор

I think a Trie would be quite efficient too, no recursion needed, probably more efficient, especially with some optimization with symmetry

pierreollivier
Автор

Its faster using lookup table. Since the input is really small.

bokunochannel
Автор

Does it have to be recursive? Couldn't you just put all the letters in an array 9x3. Then just index the array using a nested loop?

dalepeterson
Автор

Proper way is a trie. That’s basically T9 input from old mobile phone days.

mad_bad_cat
Автор

That's just cartesian product that could be implemented with 2 loops.

RomanDryndik
Автор

you could actually do this really easily with the logical programming language prolog which has built in backtrracking

boi
Автор

Wait... Have you not used this kind of phone in a while? Because... 23 is always ad. The way you would type using this kind of phone is you would press the same button multiple times to get the letter you wanted. So if you wanted "bf" you would press "223" and wait for a moment if you wanted"aaf".

I might just be misunderstanding the question.

Average-Joe
Автор

I’m confused because I thought you have to hit 2 2 2 to type C lol so to me it just looks like “ad”

stoched
Автор

This is similar to permutations of a string problem which can be solved using same approach.

swapug
Автор

Cool solution, did you also consider a nested loop?

JoostPloegmakers
Автор

This looks too complicated. Isn't easier just to to make two for loops like this? (In C)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define KEY_LETTERS_LEN (3)

int main()
{
char key_2_letters[] = "abc";
char key_3_letters[] = "def";
char combination[3];

for (size_t i = 0; i < KEY_LETTERS_LEN, i++) {
for (size_t i = 0; i < KEY_LETTERS_LEN, i++) {
combination[0] = key_2_letters[i];
combination[1] = key_3_letters[j];
combination[2] = '\0';

printf("%s\n", combination);
}
}

return 0;
}

СергейДехтярёв-ън
Автор

yeah but could you invert a binary tree for me

_Nocturio
Автор

New to code..23 is a string because of quotations or and integer?

FYIWDWYTM