우당탕탕 코딩주머니
article thumbnail
[프로그래머스] 문자열 나누기
코딩/코딩테스트 2023. 6. 21. 20:32

1. 문제 2. 코드 class Solution { public int solution(String s) { int answer = 0; int xCount = 0; int notXcount = 0; for (char c : s.toCharArray()) { if (c == s.charAt(0)) { xCount++; } else { notXcount++; } if (xCount == notXcount) { s = s.substring(xCount * 2); answer++; xCount = 0; notXcount = 0; } } if (xCount != 0 || notXcount != 0) { answer++; } return answer; } } 3. 설명 1. 카운트를 측정 하기위해 xCount, ..

article thumbnail
[프로그래머스] 가장 가까운 글자
코딩/코딩테스트 2023. 6. 18. 17:43

import java.util.*; class Solution { public int[] solution(String s) { int[] answer = new int[s.length()]; Map charToIndexMap = new HashMap(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (charToIndexMap.containsKey(ch)) { answer[i] = i - charToIndexMap.get(ch); } else { answer[i] = -1; } charToIndexMap.put(ch, i); } return answer; } } 1. 각 문자에 대해, 만약 이전에 등장한 적이 없다면 결과 배열에 -1..

article thumbnail
[프로그래머스] 크기가 작은 부분 문자열
코딩/코딩테스트 2023. 6. 14. 21:20

class Solution { public int solution(String numStr, String targetStr) { int targetLength = targetStr.length(); long targetValue = Long.parseLong(targetStr); int occurences = 0; // 길이가 targetLength인 창을 numStr에 슬라이드 for (int i = 0; i

article thumbnail
[프로그래머스] 둘만의 암호
코딩/코딩테스트 2023. 6. 14. 20:42

class Solution { public String solution(String s, String skip, int index) { StringBuilder answer = new StringBuilder(); for (char ch : s.toCharArray()) { for (int i = 0; i 'z') { ch = 'a'; } if (skip.indexOf(ch) == -1) { i++; } } answer.append(ch); } return answer.toString(); } } 각 문자에 주어진 index에 도달할때까지 문자를 1 증가 -> skip 문자열에 해당 문자가 없을때만 가능하다 문자가 'z'를 벗어나면 a로 돌아간다