일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- 편스토랑
- 백준
- 프로그래머스
- PYTHON
- 파이썬
- 프로그래머스 파이썬
- Kaggle
- 우분투
- 맥북
- 더현대서울 맛집
- AI 경진대회
- Real or Not? NLP with Disaster Tweets
- github
- 캐치카페
- dacon
- ChatGPT
- 자연어처리
- Docker
- SW Expert Academy
- 금융문자분석경진대회
- leetcode
- programmers
- 데이콘
- Git
- hackerrank
- Baekjoon
- 편스토랑 우승상품
- gs25
- 코로나19
- ubuntu
- Today
- Total
솜씨좋은장씨
[HackerRank] Dictionaries and Hashmaps: Two Strings (Python) 본문
[HackerRank] Dictionaries and Hashmaps: Two Strings (Python)
솜씨좋은장씨 2020. 3. 14. 01:29
Given two strings, determine if they share a common substring. A substring may be as small as one character.
For example, the words "a", "and", "art" share the common substring a. The words "be" and "cat" do not share a substring.
Function Description
Complete the function twoStrings in the editor below. It should return a string, either YES or NO based on whether the strings share a common substring.
twoStrings has the following parameter(s):
- s1, s2: two strings to analyze .
Input Format
The first line contains a single integer p, the number of test cases.
The following p pairs of lines are as follows:
- The first line contains string s1.
- The second line contains string s2.
Constraints
- s1 and s2 consist of characters in the range ascii[a-z].
- 1 <= p <= 10
- 1 <= | s1 |, | s2 | <= 10^5
Output Format
For each pair of strings, return YES or NO.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
Explanation
We have p = 2 pairs to check:
- s1 = "hello", s2 = "world". The substrings "o" and "1" are common to both strings.
- a = "hi", b = "world". s1 and s2 share no common substrings.
Solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the twoStrings function below.
def twoStrings(s1, s2):
answer = "NO"
s1_set = set(list(s1))
s2_set = set(list(s2))
intersection = s1_set.intersection(s2_set)
if len(intersection) != 0:
answer = "YES"
return answer
Solution 풀이
set에서 교집합을 구해주는 intersection메소드를 활용하여
교집합 결과가 비었으면 "NO" 그렇지 않으면 "YES" 를 return하도록 합니다.
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[HackerRank] Plus Minus (Python) (0) | 2020.03.16 |
---|---|
[HackerRank] Diagonal Difference (Python) (0) | 2020.03.15 |
[HackerRank] Arrays: Array Manipulation (Python) (0) | 2020.03.13 |
[leetCode] 58. Length of Last Word (Python) (2) | 2020.03.12 |
[HackerRank] Arrays: Minimum Swaps 2 (Python) (0) | 2020.03.11 |