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

14-SeongHoonC #50

Merged
merged 2 commits into from
Mar 9, 2024
Merged

14-SeongHoonC #50

merged 2 commits into from
Mar 9, 2024

Conversation

SeongHoonC
Copy link
Collaborator

πŸ”— 문제 링크

λΆˆλŸ‰ μ‚¬μš©μž

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ 20λΆ„

✨ μˆ˜λ„ μ½”λ“œ

dfs λŠ” μ§„μ§œμ§„μ§œ μ˜€λžœλ§Œμ— ν‘Έλ„€μš”..

  1. λΆˆλŸ‰ μ‚¬μš©μž λͺ©λ‘ μˆœμ„œλŒ€λ‘œ κ°€λŠ₯ν•œ μ‚¬λžŒμ„ μ°ΎμŠ΅λ‹ˆλ‹€.
  2. 이미 찾아진 μ‚¬λžŒμ΄λΌλ©΄ 더 이상 듀어가지 μ•ŠμŠ΅λ‹ˆλ‹€(κ°€μ§€μΉ˜κΈ°)ν•©λ‹ˆλ‹€.
  3. [*rodo, *rodo, ******] μ—μ„œ [frodo, crodo, frodoc] κ³Ό [crodo, frodo, frodoc] 은 같은 경우의 수 μ΄λ―€λ‘œ 쀑볡 μ œκ±°ν•©λ‹ˆλ‹€.
  • μ€‘λ³΅μ œκ±°λŠ” μ •λ ¬ ν›„ anwser Set 에 add ν•˜λŠ” μ‹μœΌλ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.
image

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

dfs ν•  λ•Œ 더 이상 κ°€λŠ₯성이 μ—†λŠ” 뢀뢄은 νƒμƒ‰ν•˜μ§€ μ•ŠλŠ” 것을 κ°€μ§€μΉ˜κΈ°λΌκ³  ν•œλ‹€.
μ‹œκ°„μ΄ˆκ³Όλ₯Ό λ©΄ν•  수 μžˆλ‹€.

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

λ‹¨μˆœ μ‘°ν•© 문제둜 잘λͺ» λ΄€λ‹€κ°€ μ‹œκ°„ 많이 λ¨Ήμ—ˆλ„€μš”.. μž¬λ°ŒλŠ” λ¬Έμ œμ˜€μŠ΄λ‹€

answer =0
list_for_cases = []
def solution(user_id, banned_id):
    
    matching_banned_id = {banned : set() for banned in banned_id}
    
    for banned in banned_id :
        for user in user_id :
            if len(user) == len(banned) :
                count_char = 0
                for idx in range(0, len(banned)) :
                    if user[idx] != banned[idx] and banned[idx]!='*' :
                        break
                    count_char += 1
                if count_char == len(banned) :
                    matching_banned_id[banned].add(user)
                    
    # print(matching_banned_id)
    dfs(matching_banned_id, banned_id,0,[])
    # print(list_for_cases)
    return answer

def dfs(match, banned_id, idx, list_for_case) :
    global answer
    global list_for_cases
    
    if idx == len(banned_id) :
        if len(list_for_cases) == 0 or not set(list_for_case) in list_for_cases :
            list_for_cases.append(set(list_for_case))
            answer+=1
        return

    for banned in match[banned_id[idx]] :
        # print(banned)
        if not banned in list_for_case:
            list_for_case.append(banned)
            dfs(match, banned_id, idx+1, list_for_case)
            list_for_case.pop()

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

Approved 잘λͺ» 올림!

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

ν™μ€€λ‹˜κ»˜μ„œ μ‘°ν•©μœΌλ‘œ λΉ„λ²Όλ³΄μ…¨λ‹€κΈΈλž˜ 저도 λΉ„λ²Όλ΄€μŠ΅λ‹ˆλ‹€.
λΉ„κ²ν•˜κ²Œ 파이썬의 λ‚΄μž₯ν•¨μˆ˜ 두 개λ₯Ό μΌμŠ΅λ‹ˆλ‹€λ§Œ..

from itertools import permutations

import re

def find(ids, patterns):
    for i in range(len(ids)):
        # λ¬Έμžμ™€ νŒ¨ν„΄μ΄ μΌμΉ˜ν•œλ‹€λ©΄?
        if re.match(patterns[i], ids[i]) and len(ids[i]) == len(patterns[i]):
            continue
        else:
            return False
    
    return True

def solution(user_id, banned_id):
    answer = []
    
    # permutations(iter, length) ν•˜λ©΄ iterμ•ˆμ—μ„œ length길이만큼의
    # μˆœμ—΄μ„ λͺ¨λ‘ λ½‘μ•„μ€λ‹ˆλ‹€.
    id_permutation = list(permutations(user_id, len(banned_id)))
    
    # μ •κ·œν‘œν˜„μ‹ μ¨λ¨ΉκΈ°μœ„ν•΄μ„œ *을 .으둜 λ°”κΏ”μ€λ‹ˆλ‹€. (λͺ¨λ“ λ¬Έμž)
    for i in range(len(banned_id)):
        banned_id[i] = banned_id[i].replace('*', '.')
    
    for perm in id_permutation:
        if find(perm, banned_id):
            # 쀑볡이 μžˆλŠ”μ§€ κ²€μ‚¬ν•˜κΈ° μœ„ν•΄μ„œ μ§‘ν•©μžλ£Œν˜•μœΌλ‘œ λΉ„κ΅ν•΄μ€λ‹ˆλ‹€.
            if not set(perm) in answer:
                answer.append(set(perm))
    
    return len(answer)

@SeongHoonC SeongHoonC merged commit eca0593 into main Mar 9, 2024
4 checks passed
@SeongHoonC SeongHoonC deleted the 14-SeongHoonC branch March 9, 2024 04:04
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