본문 바로가기

Computer Science/Algorithm with Code

[HackerRank] Preparation_Kit (11. Two Strings) [Dictionaries & Hashmaps]

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") }
}

https://www.hackerrank.com/challenges/two-strings/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps