TGViewer
پایتون | Data Science | Machine Learning پایتون | Data Science | Machine Learning @python4all_pro · 24.2K subscribers
Post #693 5K
حل سوالات استخدامی سایت leetcode.com

Task: No. 17. Letter Combinations of a Phone Number #medium

Condition:
Given a string containing the numbers 2 to 9 inclusive, return all possible combinations of letters that the number can represent. Return the answer in any order. The correspondence between numbers and letters (as on telephone buttons) is given below. Note that 1 does not match any letters.

Solution:

class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
nums.sort()
self.dfs(nums, [], res)
return res

def dfs(self, nums, path, res):
res.append(path)
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i-1]:
continue
self.dfs(nums[i+1:], path + [nums[i]], res)


Explanation:
Sorting:

First we sort the nums array. Sorting makes it easy to handle duplicates because similar items will be placed next to each other.
Recursive dfs method:

The dfs method is used to recursively construct all possible subsets. In dfs, path represents the current subset, and res is a list of all subsets.
Adding a subset to the result:

At each level of recursion, we add the current subset of path to the result of res.
Handling duplicates:

Before continuing the recursion, we check whether the current element nums[i] is a duplicate of the previous element. If so, skip it to avoid adding identical subsets to res.
Recursive construction of subsets:

For each element in nums, starting at the current index, we call dfs on the next elements of the array (nums[i+1:]). This means that we consider subsets that include the current element, and continue to build subsets without the current element.
Path (path) and compartment (nums):

Each time dfs is called, a new subset is created by adding the current element to path. This new list is then passed to the next level of recursion, allowing all possible subsets to be constructed.
Time and space complexity:
Time complexity: O(2^n), where n is the number of elements in nums. This is because there are 2^n subsets for an array of n elements.
Space complexity: O(2^n * n), since each of the 2^n possible subsets may require up to n elements to store.


#interview #LeetCode

🆔 @Python4all_pro
  • 👍 9
More from @python4all_pro
  1. Oct 4, 2026‌🔴 خبر فوری — انتخاب رشته هوشمند و رایگان با «مسیر» 🔥 «مسیر» نرم‌افزار هوشمند و کاملاً ر…
  2. Oct 3, 2026🟣کتاب Oxford word skills یک بار جایزه بهترین کتاب آموزش زبان انگلیسی سال را دریافت کرده.…
  3. Oct 2, 2026💥 ۲۰۰ هزار تومان تخفیفِ بیشتر علاوه بر تخفیفِ ۷۵ درصدی موجود در سایت | اشتراک یک ساله فرا…
  4. Sep 29, 2026What better way to understand a powerful tool like Claude Code than to build your own vers…
  5. Sep 28, 2026🧨 ۲۰۰ آموزش جدید دیگر، جایگزین آموزش‌های قبلی شد... 💯 ۴۰۰+۲۰۰ آموزش در فرادرس، هر آموزش…
  6. Sep 28, 202625 GitHub Repositories Every Python Developer Should Know! Want to improve your Python ski…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →