Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 10987] ๋ชจ์Œ์˜ ๊ฐœ์ˆ˜ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 10987] ๋ชจ์Œ์˜ ๊ฐœ์ˆ˜ (Swift)

๋น„๋น„ bibi 2023. 1. 26. 11:57

[๋ฐฑ์ค€ 10987] ๋ชจ์Œ์˜ ๊ฐœ์ˆ˜ (Swift)

10987๋ฒˆ: ๋ชจ์Œ์˜ ๊ฐœ์ˆ˜

์ž…๋ ฅ ๋ฌธ์ž์—ด์—์„œ ๋ชจ์Œ(a, e, i, o, u)์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์„œ ์ถœ๋ ฅํ•˜๋Š” ๋‹จ์ˆœํ•œ ๋ฌธ์ œ์ด๋‹ค.

ํ’€์ด๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋ณด์•˜๋‹ค.

  1. ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ๋Œ๋ฉฐ ์–ด๋–ค ๋ฌธ์ž๊ฐ€ a, e, i, o, u ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ธฐ (== ์—ฐ์‚ฐ์ž ํ™œ์šฉ)
  2. a, e, i, o, u ๋ฅผ ๋‹ด์€ ๋ชจ์Œ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ๋Œ๋ฉฐ ์–ด๋–ค ๋ฌธ์ž๊ฐ€ ๋ชจ์Œ ๋ฐฐ์—ด์— ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ (contains(_:) ํ™œ์šฉ)

๋‘ ํ’€์ด๋ฅผ ๋ชจ๋‘ ์ž‘์„ฑํ–ˆ๊ณ  ๋‘˜ ๋‹ค ๋ชจ๋‘ ์ž˜ ํ’€๋ ธ๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ์˜๋ฌธ์ด ์ƒ๊ฒผ๋˜ ๋ถ€๋ถ„์€ ๋‘ ํ’€์ด์˜ ์‹คํ–‰์‹œ๊ฐ„์ด ์™„์ „ํžˆ ๊ฐ™์•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๋‚˜๋Š” ๋‹น์—ฐํžˆ ํ’€์ด1์ด ๋” ๋น ๋ฅผ ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜๊ณ  ํ’€์ด1์„ ๋จผ์ € ์‹œ๋„ํ•˜๊ณ , ๋งž์€ ๊ฑธ ํ™•์ธํ•œ ๋’ค ํ’€์ด2๋„ ์‹œ๋„ํ–ˆ๋Š”๋ฐ ์‹คํ–‰์‹œ๊ฐ„์ด ๊ฐ™์•„์„œ ๋†€๋ž๋‹ค.

๊ทธ๋ž˜์„œ ๋‘˜์˜ ์‹คํ–‰์‹œ๊ฐ„์„ ์ž์„ธํžˆ ํƒ๊ตฌํ•ด ๋ณด์•˜๋‹ค.

ํ’€์ด 1 (== ์—ฐ์‚ฐ์ž ํ™œ์šฉ)


let input = readLine()!
var answer = 0
for char in input {
    if char == "a" || char == "e" || char == "i" || char == "o" || char == "u" {
        answer += 1
    }
}
print(answer)

ํ’€์ด1์—์„œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ k๋ผ๋ฉด, ๋น„๊ตํ•ด์•ผ ํ•  ๋ชจ์Œ์˜ ๊ฐœ์ˆ˜๋Š” 5๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์•…์˜ ๊ฒฝ์šฐ ์‹คํ–‰์‹œ๊ฐ„์€ k*5๊ฐ€ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ๋‚ด๊ฐ€ ํ’€์ด1์ด ๋” ๋น ๋ฅผ ๊ฑฐ๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ๋˜ ์ด์œ ๋Š” contains(_:) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n) ์ด๊ณ , ๋”ฐ๋ผ์„œ ๋‹น์—ฐํžˆ ์ด๋ณด๋‹ค๋Š” == ์—ฐ์‚ฐ์ž๊ฐ€ ๋น ๋ฅผ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ’€์ด1์—์„œ๋Š” == ์—ฐ์‚ฐ์„ || ๋กœ ์ด์–ด์„œ ๋‹ค์„ฏ ๋ฒˆ ์‚ฌ์šฉํ•˜๊ธด ํ•˜์ง€๋งŒ, || ์—ฐ์‚ฐ์˜ ํŠน์„ฑ์ƒ ๋งจ ์™ผ์ชฝ๋ถ€ํ„ฐ ์‹œํ–‰ํ•ด ํ•˜๋‚˜๋ผ๋„ true์ด๋ฉด ์ „์ฒด ๊ฒฐ๊ณผ๋„ true์ด๋ฏ€๋กœ ๋” ์ด์ƒ ๋น„๊ตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๊ฑธ short-circuit evaluation ๋ผ๊ณ  ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. (์ฐธ๊ณ  : Swift ๊ณต์‹ ๋ฌธ์„œ)

๊ทธ๋ž˜์„œ ์กฐ๊ฑด๋ฌธ (char == "a" || char == "e" || char == "i" || char == "o" || char == "u") ์„ ๋๊นŒ์ง€ ํ™•์ธํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ ์•„๋‹ˆ๋ผ๋ฉด, k*5๋ณด๋‹ค๋Š” ๋น ๋ฅธ ์‹คํ–‰์‹œ๊ฐ„์„ ๊ฐ€์งˆ ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ’€์ด 2 (contains(_:) ํ™œ์šฉ)

let input = readLine()!
let aeiou: [Character] = ["a", "e", "i", "o", "u"]
var answer = 0
for char in input {
    if aeiou.contains(char) {
        answer += 1
    }
}
print(answer)

ํ’€์ด2์—์„œ ์‚ฌ์šฉํ•˜๋Š” contains(_:) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n) ์ด๋‹ค. ์ด๋•Œ n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด์ด๋ฉฐ, ์—ฌ๊ธฐ์„œ๋Š” aeiou ์˜ ๊ธธ์ด์ธ 5๊ฐ€ ๋œ๋‹ค.

๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ๋Œ๋ฉฐ contains๋ฅผ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ k์ผ ๋•Œ ์‹คํ–‰์‹œ๊ฐ„์€ k * 5์ด๋‹ค.

์ด ๋•Œ ๋‚ด๊ฐ€ ๊ฐ„๊ณผํ•œ ๊ฒƒ์€ contains์˜ ๋™์ž‘์ด๋‹ค.

๊ณต์‹๋ฌธ์„œ์—๋Š” contains(_:) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์ด๋ผ๊ณ ๋งŒ ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ contains ์—ญ์‹œ ๋ฐฐ์—ด์˜ ์•ž ์š”์†Œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๋ฏ€๋กœ, ํƒ์ƒ‰ํ•  ์š”์†Œ๊ฐ€ ์•ž์— ์žˆ๋Š” ๊ฒฝ์šฐ n๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋‹จ์ˆœํ™”ํ•ด ํ‘œํ˜„ํ•˜์ž๋ฉด [0, 0, 0, 0, 1]์—์„œ contains(1)๋กœ 1์„ ์ฐพ๋Š” ๊ฒƒ๋ณด๋‹ค [1, 0, 0, 0, 0]์—์„œ contains(1)๋กœ 1์„ ์ฐพ๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฐพ๋Š” ์š”์†Œ๊ฐ€ ๋งจ ์•ž์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

(์ž์„ธํ•œ ๋‚ด์šฉ์€ contains(_:) ๋ฌธ์„œ์™€ ํ•จ๊ป˜ ํ•ด์„ํ•œ ์ด ๊ธ€์— ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค)

๊ฒฐ๋ก 

== ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ’€์ด1์€, ์ตœ์•…์˜ ๊ฒฝ์šฐ ์กฐ๊ฑด๋ฌธ (char == "a" || char == "e" || char == "i" || char == "o" || char == "u") ์„ ์ „๋ถ€ ๋น„๊ตํ•ด์•ผ ํ•˜์ง€๋งŒ, ์•ž์˜ ์กฐ๊ฑด๋ฌธ์ด ํ•˜๋‚˜๋ผ๋„ true์ธ ๊ฒฝ์šฐ short-circuit evaluation์— ์˜ํ•ด ๋‚˜๋จธ์ง€ ์กฐ๊ฑด๋ฌธ์€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

contains(_:)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ’€์ด2 ์—ญ์‹œ, ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ์Œ ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ๋น„๊ตํ•ด์•ผ ํ•˜์ง€๋งŒ, ์ฐพ๋Š” ์š”์†Œ๊ฐ€ ์•ž์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ ๋‚˜๋จธ์ง€ ๋น„๊ต๋ฅผ ๋ฉˆ์ถ”๊ณ  ์ฆ‰์‹œ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด๋ฌธ ๋น„๊ต ์ˆœ์„œ(char == "a" || char == "e" || char == "i" || char == "o" || char == "u") ์™€ contains๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ ์ˆœ์„œ(["a", "e", "i", "o", "u"])๊ฐ€ ๊ฐ™์€ ํ•œ ๋‘˜์€ ๋™์ผํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด๋‹ค!