Swift Data Structure And Algorithm/String Algorithm

๋ฌธ์ž์—ด ํฌํ•จ๊ด€๊ณ„ ์กฐ์‚ฌ

youngjaeLee1026 2022. 3. 10. 19:24

1. ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 21 58

2. ์ž…์ถœ๋ ฅ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 22 12

3. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 22 25

4. ๋ฌธ์ œ ์„ค๊ณ„

  1. ๊ธด ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€๋ น, watermelon์ธ ๊ฒฝ์šฐ index๋ฅผ 0~4, 1 ~ 5, 2 ~ 6, 3 ~ 7, 4 ~ 8, 5 ~ 9 ํŒจํ„ด์œผ๋กœ ์ˆœํšŒํ•˜๊ณ ,
  2. ์งง์€ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€๋ น, melon์ธ ๊ฒฝ์šฐ ์œ„ index์•ˆ์—์„œ ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ™์€ ๊ฐœ์ˆ˜๊ฐ€ ์งง์€ ๋ฌธ์ž์—ด ๊ธธ์ด์™€ ๊ฐ™๋‹ค๋ฉด,
  3. ๊ธด ๋ฌธ์ž์—ด์— ์งง์€ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋จ์„ ์•Œ ์ˆ˜ ์žˆ์Œ

5. ์ „์ฒด ์ฝ”๋“œ

//
//  main.swift
//  IncludeString
//
//  Created by ์ด์˜์žฌ on 2022/03/10.
//MARK: - ๋ฌธ์ž์—ด ํฌํ•จ๊ด€๊ณ„ ์กฐ์‚ฌ

//MARK: - Framework
import Foundation

//MARK: - Function
func solution() -> Void {
    //MARK: - Input
    guard let A: String = readLine() else { return }
    guard let B: String = readLine() else { return }
    var flag: Bool = false
    
    //MARK: - Process
    if A.count < B.count {
        flag = false
    } else {
        for i in 0...A.count - B.count {
            var sameCount: Int = 0
            
            for j in 0..<B.count {
                sameCount += B[B.index(B.startIndex, offsetBy: j)] == A[A.index(A.startIndex, offsetBy: i + j)] ? 1 : 0
            }

            if sameCount == B.count {
                flag = true
                break
            }
        }
    }
    
    //MARK: - Output
    print(flag ? "YES" : "NO")
}
solution()

 

์ „์ฒด์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.