728x90
An email address such as "John.Smith@example.com"
is made up of a local part ("John.Smith"
), an "@"
symbol, then a domain part ("example.com"
).
The domain name part of an email address may only consist of letters, digits, hyphens and dots. The local part, however, also allows a lot of different special characters. Here you can look at several examples of correct and incorrect email addresses.
Given a valid email address, find its domain part.
Example
- For
address = "prettyandsimple@example.com"
, the output should befindEmailDomain(address) = "example.com"
; - For
address = "fully-qualified-domain@codesignal.com"
, the output should befindEmailDomain(address) = "codesignal.com"
.
Input/Output
- [execution time limit] 20 seconds (swift)
- [input] string address
- Guaranteed constraints:
10 ≤ address.length ≤ 50
. - [output] string
설명
이메일 문자열에서 도메인만 가져와라
풀이
문자열에서 "@"의 위치를 찾아서 뒤에오는 문자들을 리턴
func findEmailDomain(address: String) -> String {
var atIdx = 0
if let range = address.range(of: "@", options: .backwards) {
atIdx = address.distance(from: address.startIndex, to: range.lowerBound)
}
let cut = address.index(address.startIndex, offsetBy: atIdx+1)
return String(address[cut...])
}
다른 사람 풀이
func findEmailDomain(address: String) -> String {
return address.components(separatedBy: "@").last!
}
문자열 사용을 잘하는 사람들 풀이를 보면 항상 배열의 first와 last를 활용 하는 듯 하다.
문자열을 "@"기준으로 분리하여 마지막 요소만 리턴하면 "@"의 위치가 어디에 있든 항상 올바른 도메인이 추출된다.
728x90
'알고리즘 > 코드시그널' 카테고리의 다른 글
Codesignal - buildPalindrome (0) | 2021.05.06 |
---|---|
Codesignal - firstDigit (0) | 2021.05.06 |
Codesignal - extractEachKth (0) | 2021.05.03 |
Codesignal - evenDigingsOnly (0) | 2021.05.03 |
Codesignal - digitDegree (0) | 2021.05.02 |
댓글