Given two strings, determine if they share a common substring. A substring may be as small as one character.
Example
a1 = 'and', a2 = 'art'
These share the common substring a.
a1 = 'be', a2 = 'cat'
These do not share a substring.
Function Description
Complete the function twoStrings in the editor below.
twoStrings has the following parameter(s):
- string s1: a string
- string s2: another string
Returns
- string: either YES or NO
set.union(array) // {"a", "b", "c", "d"}
set.intersect(array) // {"a", "b"}
set.subtract(array) // {"c"}
set.exclusiveOr(array)
func twoStrings(s1: String, s2: String) -> String {
let str = Array(Set(s1)) + Array(Set(s2))
let setStr = Set(str)
if str.count == setStr.count { return("NO") }
else { return("YES") }
}
'Computer Science > Algorithm with Code' 카테고리의 다른 글
[HackerRank] Preparation_Kit (15. Mark and Toys) [Sorting] (0) | 2024.09.28 |
---|---|
[HackerRank] Preparation_Kit (10. Ransom Note) [Dictionaries & Hashmaps] (2) | 2024.09.27 |
[HackerRank] Preparation_Kit (08. Minimum Swaps 2) [Array] (1) | 2024.09.26 |
[HackerRank] Preparation_Kit (06. Left Rotation) [Array] (0) | 2024.09.21 |
[HackerRank] Preparation_Kit (05. 2D Array - DS) [Array] (0) | 2024.09.19 |