Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17-mong3125 #57

Merged
merged 1 commit into from
Jun 27, 2024
Merged

17-mong3125 #57

merged 1 commit into from
Jun 27, 2024

Conversation

mong3125
Copy link
Collaborator

@mong3125 mong3125 commented May 2, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ฝ”๋“œ

import java.util.HashSet;

class Solution {
    public boolean solution(String[] phone_book) {
        
        HashSet<String> set = new HashSet<>();
        
        for (String phoneNum : phone_book) {
            set.add(phoneNum);
        }
        
        for (String phoneNum : phone_book) {
            for (int i = 0; i < phoneNum.length(); i++) {
                if (set.contains(phoneNum.substring(0, i))) return false;
            }
        }
        
        return true;
    }
}

โ“ ์ฝ”๋“œ ์„ค๋ช…

HashSet์— ๋ชจ๋“  ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค.

๋ชจ๋“  ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ
    ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋ชจ๋“  subString์ด set์— ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ 
        ์กด์žฌํ•œ๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์•„๋‹ˆ๋ผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

  • HashSet์˜ ์กฐํšŒ์†๋„๋Š” ๊ฑฐ์˜(์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์„๋•Œ) O(1) ์ •๋„๋กœ ๋น ๋ฅด๋‹ค.
  • ์ด๋Š” ๋ชจ๋“  subString์„ ์ „๋ถ€ ์กฐํšŒํ•˜๋”๋ผ๋„ ๊ดœ์ฐฎ์„ ์ •๋„์ด๋‹ค.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–•๋ณด๊ณ  ํ’€๋ ค๋‹ค๊ฐ€ ํ˜ธ๋˜๊ฒŒ ๋งž์€ ๋ฌธ์ œ์˜€๋„ค์š”.. ํ•ญ์ƒ ํ•ด์‹œ๋„ ์—ผ๋‘ํ•ด๋‘๊ณ  ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ์—ฐ์Šต์ด ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์…จ๋„ค์š”. substring๋ฅผ ์‚ฌ์šฉํ•œ๊ฒŒ ์ธ์ƒ ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํ’€์ด๋กœ ํ’€์€ C++ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

#include <unordered_set>
#include <string>
#include <vector>
using namespace std;

bool solution(vector<string> phone_book) {

    unordered_set <string> set;

    for (string number : phone_book) {
        set.insert(number);
    }

    for (string number : phone_book) {
        for (int size = 0; size < number.size(); size++) {
            if (set.find(number.substr(0, size)) != set.end()) return false; 
        }
    }

    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants