[프로그래머스] 접두사인지 확인하기

devel_liz·2024년 12월 19일
1

코딩테스트

목록 보기
32/45

🗒 문제



📝 나의 문제풀이

class Solution {
    fun solution(my_string: String, is_prefix: String) = 
    if(my_string.startsWith(is_prefix)) 1 else 0
}

📝 다른 사람의 문제 풀이

class Solution {
    fun solution(my_string: String, is_prefix: String): Int = if (my_string.startsWith(is_prefix)) 1 else 0
}

🖊 문제 풀이 시 알면 좋을 것

  • startsWith()

    • 접두사인지 확인하려면 startsWith() 메소드를 사용합니다.

    • 설명: 문자열이 특정 접두사로 시작하는지 확인합니다.

    • 반환값: Boolean (true 또는 false)

      val text = "HelloWorld"
      
      // "Hello"로 시작하는지 확인
      println(text.startsWith("Hello")) // true
      
      // "World"로 시작하는지 확인
      println(text.startsWith("World")) // false

📌 마치며

이전에 풀었던 문제 중에 접미사인지 확인하는 문제랑 흡사하여 endsWith()메소가 떠올라 그 반대인 startsWith()메소드를 사용해 풀었다.

profile
Android zizon

0개의 댓글

관련 채용 정보