saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 알고리즘
  • 문제해결능력
  • 취준
  • 딥러닝
  • 온라인저지
  • 개발자
  • 리트코드
  • DFS
  • 개발자 취업준비
  • BFS
  • 백준
  • 알고리즘문제해결
  • Python
  • 파이썬
  • two pointer
  • c++
  • 릿코드
  • 딕셔너리
  • LeetCode
  • 취업준비

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

a+b=c 숫자식에서 각 숫자의 한자리 수 치환을 통해 식을 맞게 만드는 방법

2022. 3. 7. 07:09

a + b = c 일때, 각 숫자 a, b, c 중 하나의 숫자를 바꿔서 식을 맞게 만들 수 있는 갯수를 구하기

바꿀 수 있는 숫자는 1 ~ 9 까지이며, 0은 사용할 수 없다. 

a 를 바꿔서 a 와 b 의 합이 c 가 되도록, b 를 바꿔서 a 와 b 의 합이 c 가 되도록, 혹은 c 를 바꿔서 a 와 b 의 합이 c 가 되도록 고쳐야 한다. 

각 숫자의 자릿수는 상관없이 입력되며, 하나의 숫자에서 한 자릿수만 수정할 수 있다. 예를 들어 1 -> 9 로 바꾸거나, 123 -> 143 으로 바꿀 수 있다. 

 

 

st = '1121+10010=11140'

def solution(st):
    a, temp = st.split('+')
    b, c = temp.split('=')
    inta, intb, intc = int(a), int(b), int(c)
    res = 0
    f = 0
    if inta + intb == intc:
        res = 1
        f = 1
    if f == 0:
        lenA, lenB, lenC = len(a), len(b), len(c)
        def check(size, strNum, num, ans):
            tmpStr = strNum
            for i in range(size):
                strNum = tmpStr
                for j in range(1, 10):
                    temp = list(strNum)
                    temp[i] = str(j)
                    strNum = "".join(temp)
                    if int(strNum) + num == ans:
                        return 1
            return 0
        def checkc(size, strNum, inta, intb):
            tmpStr = strNum
            for i in range(size):
                strNum = tmpStr
                for j in range(1, 10):
                    temp = list(strNum)
                    temp[i] = str(j)
                    strNum = "".join(temp)
                    if inta + intb == int(strNum):
                        return 1
            return 0
        res += check(lenA, a, intb, intc)
        res += check(lenB, b, inta, intc)
        res += checkc(lenC, c, inta, intb)
    print(res)

solution(st)

 

 

 

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 890. Find and Replace Pattern  (0) 2022.07.29
물건 사고 파는 문제 파이썬 order, sell, return  (0) 2022.03.07
[LeedCode] 929. Unique Email Addresses 파이썬 (Easy)  (0) 2021.12.21
[LeedCode] 1167. Minimum Cost to Connect Sticks 파이썬 Medium  (0) 2021.11.06
[LeedCode]819. Most Common Word 파이썬 Easy  (0) 2021.11.04
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 890. Find and Replace Pattern
    • 물건 사고 파는 문제 파이썬 order, sell, return
    • [LeedCode] 929. Unique Email Addresses 파이썬 (Easy)
    • [LeedCode] 1167. Minimum Cost to Connect Sticks 파이썬 Medium
    saurus2
    saurus2
    Simple is Best

    티스토리툴바