Skip to content

[Algorithm] 크기가 작은 부분 문자열 #43

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 크기가 작은 부분 문자열](https://school.programmers.co.kr/learn/courses/30/lessons/147355)


💬 Idea

  1. swift는 문자열을 다루기 쉽지 않기에 우선 문자열을 배열로 변환하여 문제를 풀어야겠다고 생각했다.
  2. Array의 인덱싱을 활용하여 p.count + index의 값이 t.count보다 작거나 같은 경우에만 t의 부분 배열을 쪼개어 String으로 join해주었다.
    ⇒ Index out of range를 방지하기 위함
  3. 이 부분문자열이 나타내는 수가 p가 나타내는 수보다 작거나 같은 것이 나오는 횟수를 구하기 위해
    Array(t)[index..<index + p.count].map({ String($0) }).joined() <= p 인 경우에만 result에 횟수를 더해주었다.

💬 풀이

func solution(_ t:String, _ p:String)->Int{varresult=0for(index, _)inArray(t).enumerated(){if index + p.count <= t.count {
// ✅ joined() -> 차례로 "314" , "141", "415", "159", "592" 로 변환.
// ✅ 해당 값이 p보다 작거나 같은 경우에만 result += 1 하여 해답 도출!
ifArray(t)[index..<index + p.count].map({String($0)}).joined()<= p {
result +=1}}}return result
}

소요시간 : 10분 43초

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions