Skip to content

Conversation

@bishoe01
Copy link
Collaborator

@bishoe01 bishoe01 commented May 9, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Leetcode1839

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-09 แ„‹แ…ฉแ„’แ…ฎ 7 34 30

๋‚œ์ด๋„ Medium ์ค‘์—์„œ ์ •๋‹ต๋ฅ  ๋”ฑ ์ ˆ๋ฐ˜์ธ๊ฑฐ๋กœ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค ! ์ฐจ์ธฐ์ฐจ์ธฐ ๋‚ด๋ ค๊ฐ€๋‹ค๊ฐ€ HARD์—์„œ ์ •๋‹ต๋ฅ  ๋†’์€๊ฑฐ๋กœ ๋ฐ”๊ฟ”๊ฐˆ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ท

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์–ด์ฒ˜ํ”ผ ๋ชจ์Œ๋งŒ์„ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์–ด์„œ vowels ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์คฌ์Šต๋‹ˆ๋‹ค.
canGoํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ vowels.firstIndex ํ™œ์šฉ - ๊ฐ index๋น„๊ต๋ฅผ ํ†ตํ•ด a-e-i-o-u ์ˆœ์„œ๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์ง€์ผœ์ฃผ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฐฐ์—ด์„ ์•ˆ์“ฐ๊ณ  ํ’€์–ด๋ณด๊ณ  ์‹ถ์–ด์„œ, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์ฒดํฌํ•œ ์•ŒํŒŒ๋ฒณ์„ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ previous๋ฅผ ๋งŒ๋“ค์—ˆ์–ด์š”. ์ดˆ๊ธฐ๊ฐ’์ด z์ธ ์ด์œ ๋Š” nil์„ ์“ฐ๊ธฐ ์‹ซ์–ด์„œ,, ์–ด์ฒ˜ํ”ผ ๋ชจ์Œ๋งŒ ๋‚˜์˜ค๋‹ˆ๊นŒ 'z'๋ฅผ ํ†ตํ•ด์„œ ์‹œ์ž‘์ ์„ ์žก์•„์ฃผ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. (๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”..!)

๋ฐ˜๋ณต๋ฌธ๋กœ์ง

  1. word์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ z ์ผ๋•Œ(์ฒ˜์Œ์ด๊ฑฐ๋‚˜, ๋Œ๋‹ค๊ฐ€ ๋Š๊ฒผ์„๋•Œ) ์— ๋Œ€ํ•ด์„œ a๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ continue์น˜๋ฉด์„œ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค !
  2. canGo๋ฅผ ํ†ตํ•ด์„œ ์ด์ „ ์• ๋ž‘ ๊ฐ™์€ ๊ฐ’์ด๊ฑฐ๋‚˜, ๋ฐ”๋กœ ๋‹ค์Œ ๋ชจ์Œ์ด๋ผ๋ฉด count+1ํ•˜๊ณ  previous๋ฅผ ํ˜„์žฌ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค ! (์‚ฌ์‹ค์ƒ ๋ฐฐ์—ด๋กœ ๋”ฐ์ง€๋ฉด ๋ฐฐ์—ด.last๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”.
  3. previous == u ์ด ๋ถ€๋ถ„์ด ์ดํ•ด์•ˆ๋˜์‹ค ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ๊ฐ™์€๋ฐ (์˜ˆ๋ฅผ๋“ค์–ด aeio๊นŒ์ง€ ๋‚˜์˜ค๊ณ  u์—†์ด ๋Š๊ฒจ๋ฒ„๋ฆฌ๋ฉด ์นด์šดํŒ…์„ ํ•ด์ฃผ๋ฉด ์•ˆ๋˜๋‹ˆ๊นŒ)
    ๋ชจ์Œ์ด ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์™€์•ผํ•˜๋‹ˆ, ๋งˆ์ง€๋ง‰๊ฐ’์ด u ๋„ค? ๊ทธ๋Ÿฌ๋ฉด ๋ชจ์Œ ๋‹ค ๋“ค์–ด์žˆ๊ฒ ๋„ค?๋ฅผ ์ง์ž‘ํ•˜๊ณ  answer ๊ฐ’์„ ๊ฑด๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !
  4. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฐ์—ด์ž์ฒด๊ฐ€ ๋๋‚˜๋ฒ„๋ ธ๋Š”๋ฐ, ์™„์„ฑ์ด ๋˜์–ด์žˆ์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ, ์ด์ œ๋Š” ๋ฐ˜๋ณต๋ฌธ ๋ฐ–์—์„œ ๋๊ฐ’์ด u์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ , answer๊ฐ’์„ ํ•œ๋ฒˆ ๋” ๊ฑด๋“œ๋ ค์ค๋‹ˆ๋‹ค ! , ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ๋ƒฅ answer ๋!
if previous == "u" {
            return max(answer, count)
        }
        else {
            return answer
        }

์ด ๋ถ€๋ถ„์— ์กฐ๊ฑด๋ฌธ ์•ˆ๋„ฃ๊ณ  ๊ทธ๋ƒฅ ํ•ญ์ƒ return max(answer, count) ํ•ด์ฃผ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹ˆ์•ผ? ํ•˜์‹ค ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ

๋ฐ˜๋ก€

aaaaa ๋ผ๋Š” ๋ฐ˜๋ก€์— ๋Œ€ํ•ด์„œ 0์ด ์•„๋‹Œ 5๋ฅผ ๋‚ด๋ฑ‰์Šต๋‹ˆ๋‹ค !

ํ•œ๋ฒˆ์— ๊ผผ๊ผผํ•˜๊ฒŒ ํ’€์–ด์•ผํ•˜๋Š”๋ฐ, ๊ณ„์† ์ œ์ถœ-๋ฐ˜๋ก€ ํ•ด๊ฒฐ-์ œ์ถœ-๋ฐ˜๋ก€ ํ•ด๊ฒฐ์„ ๋ฐ˜๋ณตํ•˜๋„ค์š”..! ๋‹ค์Œ์—๋Š” ํ•œ๋ฒˆ์— ํ•ด๊ฒฐ๋˜๋Š”๊ฑธ ๋ชฉํ‘œ๋กœ ์ข€ ๋” ์‹ ์ค‘ํ•˜๊ฒŒ ์งœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ํ•€๊ณผ ๊ฐ™์ด ๋ญ”๊ฐ€ ๊น”๋”ํžˆ ๋ชปํ’€๊ณ  ์—„์ฒญ ์ง๊ด€์ ์œผ๋กœ ํ•„์š”ํ•œ๊ฑฐ ๋ฐ”๋กœ๋ฐ”๋กœ ๊ฐ€์ ธ๋‹ค ์“ฐ๋ฉด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ… ..

class Solution {
    func longestBeautifulSubstring(_ word: String) -> Int {
        var previous: Character? 
        var count = 0
        var answer = 0
        var currentVowelIndex = 0 // 0: 'a', 1: 'e', 2: 'i', 3: 'o', 4: 'u'
        
        for current in word {
            if current == "a" && currentVowelIndex == 0 {
                count = 1
                previous = "a"
                currentVowelIndex = 1
            } else if current == "e" && currentVowelIndex == 1 {
                count += 1
                previous = "e"
                currentVowelIndex = 2
            } else if current == "i" && currentVowelIndex == 2 {
                count += 1
                previous = "i"
                currentVowelIndex = 3
            } else if current == "o" && currentVowelIndex == 3 {
                count += 1
                previous = "o"
                currentVowelIndex = 4
            } else if current == "u" && currentVowelIndex == 4 {
                count += 1
                previous = "u"
                answer = max(answer, count)
            } else if current == previous {
                count += 1
            } else {
                if current == "a" {
                    count = 1
                    previous = "a"
                    currentVowelIndex = 1
                } else {
                    count = 0
                    previous = nil
                    currentVowelIndex = 0
                }
            }
        }
        return answer
    }
}

Comment on lines +8 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๊ฑธ ๋”ฐ๋กœ ๋นผ๋‘์…จ๊ตฐ์š”!

Comment on lines +11 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil๋กœ ์•ˆํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil๋กœ ์‚ฌ์‹ค ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ, ์˜ต์…”๋„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š”๊ฒŒ ์—ฌ์ „ํžˆ ์ข€ ์–ด์ƒ‰ํ•ด์„œ ํŽธํ•œ๋Œ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์šฐํšŒํ•ด์„œ ํ–ˆ์Šต๋‹ˆ๋‹ค ใ…  ์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์งˆ์ˆ˜๋ก ์ต์ˆ™ํ•œ ๋ฐฉ์‹์„ ์ฑ„ํƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” swiftyํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ข€ ์ ๊ทน ํ™œ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ท.

Comment on lines +26 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ•จ์ˆ˜ํ™”๋ฅผ ํ•˜์…จ๊ตฐ์š”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งž์•„์š” ์•ฝ๊ฐ„ isRange dfs/bfs์—์„œ ํ•˜๋“ฏ์ด ํ™œ์šฉํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฉ‹์ง„ ์ฝ”๋“œ๋„ค์š” ํ•€.
nill์„ ์ž„์˜๋กœ ์ค˜์„œ C์–ธ์–ด์ฒ˜๋Ÿผ ๋งจ๋’ค์— null ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ์›๋ฆฌ๋„ค์š”
๋งŒ์•ฝ์— z๊ฐ€ ์•„๋‹Œ

var previous: Character? = nil
if previous == nil {
}

๋กœ ์คฌ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์šฉ ? ํƒ€์ž… ์•ˆ์ •์„ฑ์ด ์ข€ ๋” ๋ณด์žฅ๋˜๋Š” ๋А๋‚Œ๊ฐ™์•„์„œ ์ ์–ด๋ดค์Šต๋‹ˆ๋‹ท

Comment on lines +11 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil ์ฒดํฌ๋กœ z๋ฅผ ํ•˜์‹ ๊ฑฐ๊ตฐ์š”. ์ €๋„ ํ‚จ๋”๊ตฐ๊ณผ ์งˆ๋ฌธ์ด ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. nil๋กœ ์•ˆํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š” ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bishoe01 ์ €๋„ ๊ถ๊ธˆํ•ด์š” ํ•€ ใ…‹ใ…‹ใ…‹ z๋กœ ํ•˜์‹  ์ด์œ ๋Š”?!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

222222 .!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ„์— ์ ์—‡๋“ฏ์ด nil๋กœ ์‚ฌ์‹ค ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ, ์˜ต์…”๋„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š”๊ฒŒ ์—ฌ์ „ํžˆ ์ข€ ์–ด์ƒ‰ํ•ด์„œ ํŽธํ•œ๋Œ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์šฐํšŒํ•ด์„œ ํ–ˆ์Šต๋‹ˆ๋‹ค ใ… 
์˜ต์…”๋„์ด ์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์ €๋Š” ์ด๋ ‡๊ฒŒ ๋ช…์‹œ์ ์œผ๋กœ ์ œ๊ฐ€ ๋””๋ฒ„๊น…ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ์ข‹๋”๋ผ๊ตฌ์š”..! ๊ทผ๋ฐ ์˜ณ์€ ๋ฐฉ์‹์ด ์•„๋‹˜์„ ๋„ˆ๋ฌด ์ธ์ง€ํ•˜๊ณ  ์žˆ์–ด์„œ.. ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” swiftyํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ข€ ์ ๊ทน ํ™œ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ท.

Comment on lines +59 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„์„

guard let prevIdx = vowels.firstIndex(of: previous),
          let currIdx = vowels.firstIndex(of: current) else {
        return false
    }
    return currIdx >= prevIdx && currIdx - prevIdx <= 1

๋กœ ์ค‘๋ณต์กฐ๊ฑด ์ œ๊ฑฐํ•˜๊ณ , guard let์œผ๋กœ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉํ•ด์ฃผ๋ฉด ์–ด๋–จ๊นŒํ•ด์„œ ๋‚จ๊ฒจ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฐ๋ฏธ๊ฐ“ guard let ๋งˆ์Šคํ„ฐ ํ•˜์…จ๊ตฐ๋‡จ !!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์™€ ํ›จ์”ฌ ๊น”๋”ํ•˜๋„ค์—ฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ƒˆ ๋˜ ๋ ˆ๋ฒจ์—…์„ ํ•˜์…จ๊ตฐ์š” ๐Ÿ‘

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋น„์Šทํ•  ์ˆ˜ ์žˆ๋Š”? ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์Šคํƒ€์ผ๋กœ ๊ตฌํ˜„์„ ํ–ˆ๋Š”๋ฐ์š”.

์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ์‹œ์ ์—, start / count๋ฅผ ๋ฆฌ์…‹ํ•˜๋ฉด์„œ
์œˆ๋„์šฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ๊ฒ€์‚ฌํ–ˆ์Šด๋‹ค.

    func longestBeautifulSubstring(_ word: String) -> Int {
        let word = Array(word)
        var maxLength = 0
        var start = 0
        var count = 0

        for i in 1..<word.count {
            if word[i] < word[i - 1] {
                start = i
                count = 0
            } else if word[i] > word[i - 1] {
                count += 1
            }
            
            if word[start] == "a", count == 4 {
                maxLength = max(maxLength, i - start + 1)
            }
        }
        return maxLength
    }

์ฃผ์š” ๋กœ์ง

for i in 1..<word.count {
    if word[i] < word[i - 1] {
        start = i
        count = 0
    } else if word[i] > word[i - 1] {
        count += 1
    }
    
    if word[start] == "a", count == 4 {
        maxLength = max(maxLength, i - start + 1)
    }
}
  • ๋ฌธ์ž์˜ ์•„์Šคํ‚ค๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ๋Œ€์†Œ๋ฅผ ๋น„๊ตํ•˜์ž!
  • ๋ฌธ์ž ์ˆœ์„œ๊ฐ€ ๊นจ์ง„๋‹ค -> start๋ฅผ ์žฌ์„ค์ •, count ์ดˆ๊ธฐํ™”
  • count๊ฐ€ 4์ด๊ณ  ์‹œ์ž‘๋ฌธ์ž๊ฐ€ a์ธ ๊ฒฝ์šฐ์—๋งŒ ๋ทฐํ‹ฐํ’€๋ผ์ดํ”„๋กœ ใ…‡ใ…ˆํ•ด์ฃผ๊ณ  ๊ธธ์ด๋ฅผ ๊ฐฑ์‹ ํ•ด์คŒ.

Comment on lines +7 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—ฌ๊ธฐ guard๋ฌธ ๋˜๊ฒŒ ์ข‹์€๋ฐ์š”!
๊ทธ๋Ÿฐ๋ฐ ์•„๋ž˜ ํ•€์ด ์ง  ๋กœ์ง์ƒ a๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฌด์‹œํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ปค๋ฒ„๊ฐ€ ๋˜์ง€ ์•Š์œผ๋ ค๋‚˜์š”?
(์ œ๊ฐ€ ์ž˜๋ชป ์ดํ•ดํ•œ๊ฑธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค!_)
ํ˜น์€ ๊ฐ€๋…์„ฑ์ด๋‚˜ ์„  ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ž‘์„ฑํ•œ ๋ถ€๋ถ„์ผ๊นŒ์š”??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ชจ์•ผ ๊ธธ ์™„์ „ ๊น”๋”ํ•˜๋‹ค

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sep037
ํ‘ธ๋Š”๋ฐ n์ผ ๊ฑธ๋ฆผ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งž์•„์š” .. !
@giljihun a๋กœ์‹œ์ž‘ํ•˜์ง€์•Š์œผ๋ฉด ๋ฌด์‹œ๋ฅผ ํ• ๊ฑด๋ฐ, ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค. ๋””๋ฒ„๊น…์ด๋ž‘ ๊ฐ€๋…์„ฑ ๊ฐ€์ ธ๊ฐ€๋ ค๊ณ  ํ•ด์คฌ์—ˆ์Šต๋‹ˆ๋‹ค !

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ›„ํ›„ ๋ฌธ์ œ ์ž˜๋ชป์ดํ•ดํ•˜๊ณ  ํ–ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„ ๋„ˆ๋ฌด ์จ๋ฒ„๋ฆผ. ์•„๋ฆ„๋‹ค์šฐ์‹  ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•„๋‹ˆ์—ˆ๊ตฐ์š”;; ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์™€์•ผํ–ˆ์Œ. ์ „ ์ƒ๊ฐ์˜ ํ๋ฆ„๋Œ€๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์€๋ฐ ์‹œ๊ฐ„๋‚˜๋ฉด ์ตœ์ ํ™” ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.^~^

๋ฌธ์ œ๋Š” ํƒ€์ž„์–ดํƒ์œผ๋กœ ํ’€์–ด๋ณด๊ณ  ๋‹ค๋ฅธ๋ถ„๋“ค ์ฝ”๋“œ ์ฝ๋Š”๊ฒƒ์— ์‹œ๊ฐ„ ๋น„์ค‘์„ ๋Š˜๋ฆฌ๋‹ˆ๊นŒ ์žฌ๋ฐŒ๋„ค์š” ใ…‹

// ์ง„ํ–‰์ค‘์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ธธ์ด, ์•„๋ฆ„๋‹ค์šด๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด์•  ๋”ฐ๋กœ ๋‹ด์•„์•ผ ํ•จ
// ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋ชจ์Œ ์ธ๋ฑ์Šค ํ™œ์šฉํ•ด์ค˜์•ผ ํ•  ๋“ฏ
// ์ฒซ a๋‚˜์˜ค๋ฉด ํ˜„์žฌ ๊ธธ์ด, ์ธ๋ฑ์Šค 1์žก๊ณ  ์‹œ์ž‘ 
// ๊ฐ™์€ ๋ฌธ์ž ๋‚˜์˜ค๋ฉด ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒ ๋Š˜๋ ค์คŒ.
// ๋‹ค์Œ ๋ฌธ์ž๋‚˜์˜ค๋ฉด ํ˜„์žฌ ๊ธธ์ด ๋Š˜๋ ค์ฃผ๊ณ  ๋‹ค์Œ ๋ฌธ์ž select
// ๋ฐ˜๋ณต
// ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ๋‚˜์˜ค๋ฉด ์ €์žฅ ํ›„ ์ตœ๋Œ€ํฌ๊ธฐ ๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ต
// ๊ทผ๋ฐ ๋˜ ๋‚˜์˜จ๋‹ค? ์ €์žฅ ํ›„ ์ตœ๋Œ€ํฌ๊ธฐ ๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ต -> ์ด๋ถ€๋ถ„ ์ตœ์ ํ™”ํ•˜๊ณ  ์‹ถ์Œ
// ์ค‘๊ฐ„์— ๊นจ์ง€๋ฉด ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๊ธฐ.


class Solution {
    func longestBeautifulSubstring(_ word: String) -> Int {
        var currentLength = 0
        var maxLength = 0
        var vowelIndex = 0
        
        for char in word {
            if char == "a" && vowelIndex == 0 {
                currentLength = 1
                vowelIndex = 1
            }
            else if char == "a" && vowelIndex == 1 {
                currentLength += 1
            }
            else if char == "e" && vowelIndex == 1 {
                currentLength += 1
                vowelIndex = 2
            }
            else if char == "e" && vowelIndex == 2 {
                currentLength += 1
            }
            else if char == "i" && vowelIndex == 2 {
                currentLength += 1
                vowelIndex = 3
            }
            else if char == "i" && vowelIndex == 3 {
                currentLength += 1
            }
            else if char == "o" && vowelIndex == 3 {
                currentLength += 1
                vowelIndex = 4
            }
            else if char == "o" && vowelIndex == 4 {
                currentLength += 1
            }
            else if char == "u" && vowelIndex == 4 {
                currentLength += 1
                vowelIndex = 5
                maxLength = max(maxLength, currentLength)
            }
            else if char == "u" && vowelIndex == 5 {
                currentLength += 1
                maxLength = max(maxLength, currentLength)
            }
            else {
                if char == "a" {
                    currentLength = 1
                    vowelIndex = 1
                } else {
                    currentLength = 0
                    vowelIndex = 0
                }
            }
        }
        
        return maxLength
    }
}

Comment on lines +11 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bishoe01 ์ €๋„ ๊ถ๊ธˆํ•ด์š” ํ•€ ใ…‹ใ…‹ใ…‹ z๋กœ ํ•˜์‹  ์ด์œ ๋Š”?!

Comment on lines +58 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•จ์ˆ˜๋กœ ๋บ€ ๊ฑฐ ์ข‹๋„ค์š”!
+ ๋˜ ๋‹ค์‹œ ๋Œ์•„์˜จ ์ปจ๋ฒค์…˜ ๋‹ด๋‹น..... ํ•˜์ž๋ฉด ์ €๋„ Idx๋ผ๊ณ  ๋งŽ์ด ์นญํ–ˆ๋Š”๋ฐ Swift์—์„œ๋Š” ํ’€ ๋„ค ์ž„์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ด€ ๋ก€ ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ดํ›„๋กœ ์ €๋„ Index๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  ์žˆ๋‹ต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

@bishoe01 bishoe01 May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์šฐ... ๊ทธ๋ ‡๊ตฐ์š”,.... ๋ฐ”๋กœ ๋‹ค์Œ๋ฒˆ๋ถ€ํ„ฐ ๋ฐ˜์˜ํ•ด๋จน๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ด์š” ๐Ÿ‘

Comment on lines +26 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊น”๋”ํ•˜๋„ค์š”! ๋ฌธ์ž์—ด ๊นจ์กŒ์„ ๋•Œ๋ž‘ ๋ฃจํ”„ ์ข…๋ฃŒํ›„์—๋งŒ ๋ฌธ์ž์—ด ๊ธธ์ด ๋น„๊ตํ•ด์ฃผ๋ฉด ๋๋„ค์š”. ์ „ ์ดˆ์ดˆ์ดˆํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ U๋‚˜์˜ฌ๋•Œ๋งˆ๋‹ค ๋น„๊ตํ•ด์ฃผ๋Š” ๋น„ํšจ์œจ์ ์ธ ์ฝ”๋“œ ์งฐ๋‹ต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธธ์„ ์ƒ๋‹˜ ํ’€์ด (์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ)๊ฐ€ ์ฐ ์ •์„์ธ๊ฒƒ๊ฐ™์•„์š”..! ์ €๋„ ๋ง‰ ๊ตฌํ˜„ํ’€๋“ฏ์ด ์ด๊ฒƒ์ €๊ฒƒ์ฐ”๋Ÿฌ๋ณธ๊ฑฐ๋ผ...! ์•„์ง ๊ฐˆ๊ธธ์ด๋จธ๋„ค์š”.. ๐Ÿ‘

@sep037
Copy link
Collaborator

sep037 commented May 17, 2025

๋ฌธ์ œ ํ‘ธ๋Š”๋ฐ ๋ฃจ๋ฏธ๊ฐ€ ์ง€๋‚˜๊ฐ€๊ธธ๋ž˜ ๋ฃจ๋ฏธ์˜ ๋„์›€์„ ์‚ด ~ ์ง ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค ใ…Ž

//
//  main.swift
//  Leet_1839
//
//  Created by Seungeun Park on 5/16/25.
//

import Foundation

class Solution {
    func longestBeautifulSubstring(_ words: String) -> Int {
        let wordCount = words.count // ๋ฌธ์ž์—ด ์ˆซ์ž ์„ธ๊ธฐ
        var aeiouIndex : Int = 0 // a,e,i,o,u ์ค‘ ๋ช‡ ๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์„ ๊ฐ€๋ฆฌํ‚ค๋Š”์ง€
        let wordArray = Array(words)
        var charCount : Int = 1 // ์–ด์ฐจํ”ผ ํ•ด๋‹น ์ˆ˜๊ฐ€ ํฌํ•จ๋  ๊ฑฐ๋‹ˆ๊นŒ 1๋กœ ๊ณ ์ •
        var beautifulStringCount : Int = 0
        
        for i in 1..<wordCount {
            let prev = wordArray[i - 1]
            let curr = wordArray[i]
            
            if curr >= prev {
                /*
                 a ๋‹ค์Œ์œผ๋กœ a, e๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ณ ,
                 e ๋‹ค์Œ์œผ๋กœ e, i๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ณ ,
                 i ๋‹ค์Œ์œผ๋กœ i, o๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ณ ,
                 o ๋‹ค์Œ์œผ๋กœ o, u๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ณ ,
                 u ๋‹ค์Œ์œผ๋กœ๋Š” u๋งŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์›€
                 */
                charCount += 1
                if curr != prev {
                    aeiouIndex += 1 // ๋™์ผํ•œ ๊ธ€์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ๋‹ค์Œ ๋ชจ์Œ์œผ๋กœ ๋„˜์–ด๊ฐ
                }
                if aeiouIndex == 4 { // ์™„๋ฒฝํ•˜๊ฒŒ ๋‚˜์˜ด
                    beautifulStringCount = max(charCount, beautifulStringCount)
                }
            } else {
                charCount = 1
                if curr == "a" {
                      aeiouIndex = 0
                  } else {
                      aeiouIndex = 0 // ๋˜๋Š” -1, ์„ ํƒ ๊ฐ€๋Šฅ
                  }
            }
        }
        return beautifulStringCount
    }
}

Comment on lines +11 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

222222 .!

@bishoe01 bishoe01 merged commit 3454fd8 into main May 21, 2025
5 checks passed
@alstjr7437 alstjr7437 deleted the 4-bishoe01 branch May 23, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants