답안 :
public class Solution {
public int solution(String s) {
int answer = 0;
while (s.length() != 0) {
answer++;
char x = s.charAt(0);
int xcnt = 1;
int ycnt = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == x) {
xcnt++;
} else if (s.charAt(i) != x) {
ycnt++;
}
if (xcnt == ycnt) {
break;
}
}
s = s.substring(xcnt + ycnt);
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
String s1 = "abracadabra";
System.out.println(s.solution(s1));
}
}
알고리즘 설명 : 입력된 문자의 첫번째 문자와 동일문자, 틀린문자를 문자열을 순회하며 카운트 한뒤 두 카운트의 합이 같아지면 for문을 나와서 문자열을 자르고 문자열의 길이가 0이될때까지 다시 반복한다.
오랜만에 나온 쉬운 문제로 처음엔 for문 하나로 제어하려했지만 문자열의 대한 변동이 필요하다 생각해 2중 반복문으로 전환 하였음.
답안 :
SELECT Month(START_DATE)as 'MONTH',car_id as 'CAR_ID',count(*) as 'RECORDS'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where car_id in(
select car_id
from CAR_RENTAL_COMPANY_RENTAL_HISTORY where date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by 1 having count(*)>=5)
and date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by 1,2
order by 1,2 desc
틀린답안 :
SELECT Month(START_DATE)as 'MONTH',car_id as 'CAR_ID',count(car_id) as 'RECORDS'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by Month(START_DATE),car_id
Having count(car_id)>=5
order by 1,2 desc