리트코드 풀이

Leetcode 2114 풀이(파이썬, 스위프트)

ag2개발자 2022. 2. 4. 20:02

단어의 개수가 가장 많은 인덱스를 고르는 문제이다 for문 연습하기 좋은 문제

스위프트

class Solution {
    func mostWordsFound(_ sentences: [String]) -> Int {
        var x = 0 
        for i in sentences{
            x = max(x, numberOfWords(i))
        }
        return x
    }
    
    func numberOfWords(_ s: String) -> Int {
        var w = 1
        for i in s where i == " "{
            w+=1
        }
        return w
    }
}

파이썬

class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        x=0
        for i in range(len(sentences)):
            x=max(len(sentences[i].split()),x)
            
        return x