๋ฌธ์ :
https://programmers.co.kr/learn/courses/30/lessons/17677
๋์ด๋ : level 2
๋ถ์ :
์์นด๋ ์ ์ฌ๋๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๋ก, ์งํฉ์ ๊ต์งํฉ, ํฉ์งํฉ์ ๊ตฌํ๋ ๊ตฌํํ๋ ๋ฌธ์ ์๋ค. ๋ค๋ง ์งํฉ์ด์ง๋ง ์ค๋ณต๋ ์์๊ฐ ์กด์ฌํ๋ ๋ค์ค ์งํฉ์ผ๋ก ๊ตฌํํด์ผ ํด์ Set ํ์ ๋์ Array๋ฅผ ์ฌ์ฉํ๋ค.
Swift ์ฝ๋ :
//
// main.swift
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2022/04/13.
//
import Foundation
func check(_ s: String) -> Bool {
for i in s {
if i.isLetter == false {
return false
}
}
return true
}
func solution(_ str1:String, _ str2:String) -> Int {
var str1Set = [String]()
var str2Set = [String]()
let str1 = str1.lowercased()
let str2 = str2.lowercased()
for i in 1..<str1.count {
let target = str1.substring(from: i-1, to: i)
if check(target) {
str1Set.append(target)
}
}
for i in 1..<str2.count {
let target = str2.substring(from: i-1, to: i)
if check(target) {
str2Set.append(target)
}
}
var sum = str1Set.count + str2Set.count
var inter = 0
//๊ต์งํฉ
for i in str1Set.indices {
for j in str2Set.indices {
if str1Set[i] == str2Set[j] {
inter += 1
str2Set.remove(at: j)
break
}
}
}
//ํฉ์งํฉ
sum -= inter
if sum == 0 { return 65536 }
return Int(Double(inter) / Double(sum) * 65536)
}
extension String {
func substring(from: Int, to: Int) -> String {
guard from < count, to >= 0, to - from >= 0 else {
return ""
}
// Index ๊ฐ ํ๋
let startIndex = index(self.startIndex, offsetBy: from)
let endIndex = index(self.startIndex, offsetBy: to + 1) // '+1'์ด ์๋ ์ด์ : endIndex๋ ๋ฌธ์์ด์ ๋ง์ง๋ง ๊ทธ ๋ค์์ ๊ฐ๋ฆฌํค๊ธฐ ๋๋ฌธ
// ํ์ฑ
return String(self[startIndex ..< endIndex])
}
}
substring์ ๊ตฌํ๋ ๊ฒ์ ์๋ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ์๋ค !! ์์ ์ ์ฉํ ๋ฏ ํ๋ค.
https://ios-development.tistory.com/379
์คํ ๊ฒฐ๊ณผ :
'Algorithm๐ฐ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] Swift ํํ 2019 ์นด์นด์ค implementation (0) | 2022.04.14 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] Swift 2020 ์นด์นด์ค ๊ดํธ๋ณํ (0) | 2022.04.14 |
[ํ๋ก๊ทธ๋๋จธ์ค] Swift 2020 ์นด์นด์ค ์ธํด์ญ ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2022.04.08 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๊ฒ ๋๋ฒ DFS C++ (0) | 2022.03.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ stack (0) | 2022.03.25 |
๋๊ธ