๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Swift 2018 ์นด์นด์˜ค ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง implementation

by Jouureee 2022. 4. 13.

๋ฌธ์ œ :

https://programmers.co.kr/learn/courses/30/lessons/17677

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง ์—ฌ๋Ÿฌ ์–ธ๋ก ์‚ฌ์—์„œ ์Ÿ์•„์ง€๋Š” ๋‰ด์Šค, ํŠนํžˆ ์†๋ณด์„ฑ ๋‰ด์Šค๋ฅผ ๋ณด๋ฉด ๋น„์Šท๋น„์Šทํ•œ ์ œ๋ชฉ์˜ ๊ธฐ์‚ฌ๊ฐ€ ๋งŽ์•„ ์ •์ž‘ ํ•„์š”ํ•œ ๊ธฐ์‚ฌ๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. Daum ๋‰ด์Šค์˜ ๊ฐœ๋ฐœ ์—…๋ฌด๋ฅผ ๋งก๊ฒŒ ๋œ ์‹ ์ž…์‚ฌ์› ํŠœ๋ธŒ

programmers.co.kr

๋‚œ์ด๋„ : 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

 

[swift - algorithm] ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ, substring

swift์˜ String ํ˜• value type ํŠน์„ฑ c, c++, java์™€๋Š” ๋‹ค๋ฅด๊ฒŒ stringํ˜•์—์„œ ์ธ๋ฑ์Šค์— Int๋กœ ์ ‘๊ทผ ๋ถˆ๊ฐ€ swift์—์„œ๋Š” ์ธ๋ฑ์Šค ๊ฐ’์— Int๊ฐ€ ์•„๋‹Œ Index์ž๋ฃŒํ˜•์ด ์œ„์น˜ (swift๋Š” Sring์ด reference type์ด ์•„๋‹Œ value t..

ios-development.tistory.com

 

์‹คํ–‰ ๊ฒฐ๊ณผ :

๋Œ“๊ธ€