LeetCode 75: 17. Letter Combinations of a Phone Number

김준수·2024년 4월 22일

LeetCode 75

목록 보기
57/63

17. Letter Combinations of a Phone Number

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Example 2:
Input: digits = ""
Output: []

Example 3:
Input: digits = "2"
Output: ["a","b","c"]

Constraints:

  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9'].

17. 전화번호의 문자 조합

2-9를 포함하는 문자열이 주어질 때, 해당 번호로 표현될 수 있는 모든 가능한 문자 조합을 반환합니다. 답은 어떤 순서로든 반환됩니다.

아래는 전화 버튼처럼 숫자에 대응하는 문자 매핑입니다. 1은 어떤 문자에도 대응하지 않는다는 점에 유의하세요.

예제 1:
입력: digits = "23"
출력: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

예제 2:
입력: digits = ""
출력: []

예제 3:
입력: digits = "2"
출력: ["a","b","c"]

제약사항:

  • 0 <= digits.length <= 4
  • digits[i]['2', '9'] 범위의 숫자입니다.

Solution

import java.util.ArrayList;
import java.util.List;

public class Solution {
    private static final String[] KEYPAD_MAPPINGS = {
            "", // 0
            "", // 1
            "abc", // 2
            "def", // 3
            "ghi", // 4
            "jkl", // 5
            "mno", // 6
            "pqrs", // 7
            "tuv", // 8
            "wxyz" // 9
    };

    public List<String> letterCombinations(String digits) {
        List<String> results = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return results;
        }
        backtrack(results, new StringBuilder(), digits, 0);
        return results;
    }

    private void backtrack(List<String> combinations, StringBuilder currentCombination, String digits, int index) {
        if (index == digits.length()) {
            combinations.add(currentCombination.toString());
            return;
        }

        String possibleLetters = KEYPAD_MAPPINGS[digits.charAt(index) - '0'];
        for (char letter : possibleLetters.toCharArray()) {
            currentCombination.append(letter);
            backtrack(combinations, currentCombination, digits, index + 1);
            currentCombination.deleteCharAt(currentCombination.length() - 1); // backtracking step
        }
    }
}

1개의 댓글

comment-user-thumbnail
2025년 3월 20일

As someone who spends a lot of time online, I’ve always been concerned about my privacy. Whether I’m signing up for new services or just browsing the web, I realize how much of my personal information is being collected and used by large companies. One of the most frustrating things for me was the constant requirement to provide my phone number when creating new accounts. I didn’t want to give out my personal number for every single registration. Not only did it feel like an invasion of privacy, but I also started to worry about the potential for spam and unwanted marketing calls. That’s when I found GrizzlySMS and their virtual number to receive sms, and I haven’t looked back since.

I first came across GrizzlySMS when I was looking for a way to bypass phone number verification for Google and Gmail. I needed to create several new accounts for different platforms, and using my personal number seemed like a bad idea. After all, I didn’t want to risk receiving marketing messages or having my personal number linked to multiple accounts. When I read about GrizzlySMS and how they offered a virtual number to receive SMS for verification purposes, I knew I had found the perfect solution.

Setting up a virtual number through GrizzlySMS was incredibly easy. It took only a few minutes to select a number from one of the many available regions, and within seconds, I was able to receive the SMS verification codes I needed. This was such a huge relief for me because I no longer had to rely on my personal number. The process was smooth, fast, and completely hassle-free. For anyone who is concerned about privacy and security, using a virtual number to receive SMS is the ideal option.

What I really appreciate about GrizzlySMS is the flexibility and convenience it offers. Not only can you use their service for Google and Gmail verification, but you can also use it for creating accounts on social media platforms, signing up for online services, or even verifying transactions. The virtual number can be used anywhere that requires a phone number for registration. It’s the perfect solution for anyone who values their privacy and wants to keep their personal phone number out of the hands of big corporations. The ability to choose from over 100 geographical locales is also a great bonus, as it allows me to select a number from a country I’m interested in or need for specific services.

The pricing of GrizzlySMS is another reason why I decided to give it a try. The service is not only incredibly affordable but also offers unmatched value for what you get. With the low cost of using a virtual number to receive SMS, I’m able to create multiple accounts for various purposes without breaking the bank. The service is priced far lower than any other alternative I found, making it a cost-effective choice for anyone who needs multiple numbers for online verification.

One of the biggest concerns I had before using a virtual number was security. I’ve heard horror stories of people using untrusted services and having their personal information compromised. However, GrizzlySMS has proven to be completely reliable and secure. Their platform is built with privacy and security in mind, and I’ve never had an issue with my information being leaked or my number being used for anything I didn’t authorize. The company offers excellent security features that ensure your data remains safe, and their customer support team is always available to help if you have any questions or concerns.

I also found the 24/7 service to be incredibly useful. Whether I need to create an account in the middle of the night or during a busy day, I can always count on GrizzlySMS to provide a virtual number to receive SMS quickly and efficiently. The support team is also available around the clock, and I’ve found them to be incredibly helpful whenever I’ve had questions about the service. GrizzlySMS has made the process of online registration so much easier, and I can now create and manage multiple accounts without worrying about my privacy being compromised.

What’s also fantastic about GrizzlySMS is the fact that I can use the virtual numbers for multiple purposes. For example, when I was doing marketing and needed to create several accounts on different platforms, I didn’t have to worry about getting banned or flagged because I could simply switch to a different number if needed. This flexibility gives me the freedom to manage my online presence without any risk to my privacy or security. Whether you’re an entrepreneur, a marketer, or just someone who values their privacy, this service is a game-changer.

In conclusion, if you’re looking for a reliable, affordable, and secure way to create new accounts without exposing your personal phone number, GrizzlySMS is the way to go. Their virtual number to receive SMS service has transformed how I manage my online presence, and I can’t recommend it enough. With a range of features, excellent security, affordable pricing, and outstanding customer support, GrizzlySMS is hands down the best option for anyone looking to protect their privacy while still being able to access the services they need. Give it a try, and I’m sure you’ll be as impressed as I was with how simple and effective the process is.

By using GrizzlySMS, you’re not just protecting your personal information; you’re gaining the ability to manage your digital life in a secure, flexible, and cost-effective way. With their virtual number to receive SMS, you can enjoy the benefits of anonymity and privacy while still enjoying all the services and platforms the internet has to offer. If privacy matters to you, don’t hesitate – try GrizzlySMS today and experience the difference!

답글 달기