본문 바로가기
Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 문자열바꿔서찾기 // String class method

by 외계나무 2023. 12. 11.

프로그래머스 - level 0 문자열 바꿔서 찾기

class Solution {
    public int solution(String myString, String pat) {
        myString = myString.replace("A", "c").replace("B", "A").replace("c", "B");
        for(int i=0; i<=myString.length()-pat.length(); i++) {
            if(myString.substring(i, i+pat.length()).equals(pat)) {
                return 1;
            }
        }
        return 0;
    }
}

짧게 만들기

class Solution {
    public int solution(String myString, String pat) {
        myString = myString.replace("A", "c").replace("B", "A").replace("c", "B");
        return myString.contains(pat) ? 1 : 0;
    }
}

 

String class 메서드에 replace와 contains가 있다는 사실을 자꾸 잊어버림...

String class method()

  • charAt
  • compareTo
  • concat
  • indexOf
  • split
  • substring
  • toUpperCase / toLowerCase
  • trim
  • length
  • isEmpty
  • isBlank
  • startsWith / endsWith
  • equals
  • contains
  • replace / replaceAll
  • format

etc ...