https://school.programmers.co.kr/learn/courses/30/lessons/49993
1. Swift 풀이
func solution(_ skill:String, _ skill_trees:[String]) -> Int {
let skill = Array(skill)
let skillElement = Set<Character>(skill)
var answer = 0
for target in skill_trees {
let target = Array(target)
var sPoint = 0, tPoint = 0
var isValid = true
while sPoint < skill.count && tPoint < target.count {
if skill[sPoint] == target[tPoint] {
sPoint += 1
tPoint += 1
}
else if skillElement.contains(target[tPoint]) {
isValid = false
break
}
else {
tPoint += 1
}
}
if isValid {
answer += 1
}
}
return answer
}
2. Python 풀이
def solution(skill, skill_trees):
skill = list(skill)
skillElement = set(skill)
answer = 0
for target in skill_trees:
target = list(target)
sPoint = 0
tPoint = 0
isValid = True
while sPoint < len(skill) and tPoint < len(target):
if skill[sPoint] == target[tPoint]:
sPoint += 1
tPoint += 1
elif target[tPoint] in skillElement:
isValid = False
break
else:
tPoint += 1
if isValid :
answer += 1
return answer
[프로그래머스] 오픈채팅방 (0) | 2024.01.10 |
---|---|
[프로그래머스] 롤케이크 자르기 (0) | 2024.01.09 |
[프로그래머스] 땅따먹기 (1) | 2024.01.04 |
[프로그래머스] 뒤에 있는 큰 수 찾기 (0) | 2024.01.03 |
[프로그래머스] 단어 변환 (0) | 2024.01.02 |