Replies: 4 comments
개인정보 수집 유효기간해결 방법
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
// 오늘 날짜를 연월일로 나누고 -> 일수로 바꿔버림
String[] t = today.split("\\.");
int todayDay = Integer.parseInt(t[0]) * 12 * 28 + Integer.parseInt(t[1]) * 28 + Integer.parseInt(t[2]);
// 약관 저장할 배열 (A~Z만 있으니까 배열로도 가능)
int[] termMonths = new int[26];
for (String term : terms) {
String[] split = term.split(" ");
char type = split[0].charAt(0); // 'A', 'B'...
int months = Integer.parseInt(split[1]);
termMonths[type - 'A'] = months;
}
// 정답 저장할 리스트
List<Integer> answerList = new ArrayList<>();
// 개인정보 돌면서 검사
for (int i = 0; i < privacies.length; i++) {
String[] split = privacies[i].split(" ");
String[] date = split[0].split("\\.");
char type = split[1].charAt(0);
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
// 수집 날짜를 일수로 바꾼 다음 + 유효기간 더함
int collected = year * 12 * 28 + month * 28 + day;
int expireDay = collected + (termMonths[type - 'A'] * 28);
// 오늘 날짜랑 비교
if (expireDay <= todayDay) {
answerList.add(i + 1); // 번호는 1번부터 시작
}
}
// 리스트를 배열로 바꿔서 리턴
int[] answer = new int[answerList.size()];
for (int i = 0; i < answerList.size(); i++) {
answer[i] = answerList.get(i);
}
return answer;
}
}신규 아이디 추천해결방법
class Solution {
public String solution(String new_id) {
// 1단계: 전부 소문자로 바꿈
String id = new_id.toLowerCase();
// 2단계: 알파벳 소문자, 숫자, -, _, . 빼고 다 지움
id = id.replaceAll("[^a-z0-9-_.]", "");
// 3단계: 마침표 여러 개면 하나로 줄임
id = id.replaceAll("[.]{2,}", ".");
// 4단계: 마침표가 앞이나 뒤에 있으면 제거
id = id.replaceAll("^[.]|[.]$", "");
// 5단계: 빈 문자열이면 "a" 넣음
if (id.equals("")) {
id = "a";
}
// 6단계: 16자 이상이면 15자만 남기고, 끝에 . 있으면 제거
if (id.length() >= 16) {
id = id.substring(0, 15);
id = id.replaceAll("[.]$", "");
}
// 7단계: 2자 이하이면 마지막 글자를 반복해서 3자 만듦
while (id.length() < 3) {
id += id.charAt(id.length() - 1);
}
return id;
}
} |
0 replies
📌 개인정보 수집 유효기간
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> list = new ArrayList<>();
Map<String, Integer> periodMap = new HashMap<>();
for (int i = 0; i < terms.length; i++) {
String[] str = terms[i].split(" ");
periodMap.put(str[0], Integer.parseInt(str[1]));
}
Calendar todayCal = toCalendar(today);
String[][] privacy = new String[privacies.length][2];
for (int i = 0; i < privacies.length; i++) {
String[] str = privacies[i].split(" ");
privacy[i][0] = str[0];
privacy[i][1] = str[1];
Calendar privacyCal = toCalendar(privacy[i][0]);
privacyCal.add(Calendar.MONTH, periodMap.get(privacy[i][1]));
privacyCal.add(Calendar.DATE, -1);
if (privacyCal.before(todayCal)) {
list.add(i + 1);
}
}
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
public Calendar toCalendar(String dateStr) {
String[] dateSplit = dateStr.split("\\.");
int year = Integer.parseInt(dateSplit[0]);
int month = Integer.parseInt(dateSplit[1]);
int day = Integer.parseInt(dateSplit[2]);
Calendar date = Calendar.getInstance();
date.set(year, month + 1, day);
return date;
}
}📌신규 아이디 추천
class Solution {
public String solution(String new_id) {
StringBuilder sb = new StringBuilder();
sb.append(new_id.toLowerCase());
for (int i = 0; i < sb.length(); i++) {
char alphabet = sb.charAt(i);
if (!((alphabet >= 'a' && alphabet <= 'z') || (alphabet >= '0' && alphabet <= '9') ||
alphabet == '-' || alphabet == '_' || alphabet == '.')) {
sb.deleteCharAt(i--);
}
}
for (int i = 1; i < sb.length(); i++) {
if (sb.charAt(i) == '.' && sb.charAt(i - 1) == '.') {
sb.deleteCharAt(i--);
}
}
while (sb.length() > 0 && sb.charAt(0) == '.') {
sb.deleteCharAt(0);
}
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '.') {
sb.deleteCharAt(sb.length() - 1);
}
if (sb.length() >= 16) {
sb.setLength(15);
}
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '.') {
sb.deleteCharAt(sb.length() - 1);
}
if (sb.length() == 0) {
sb.append('a');
}
while (sb.length() < 3) {
sb.append(sb.charAt(sb.length() - 1));
}
return sb.toString();
}
} |
0 replies
📌 신규 아이디 추천풀이
class Solution {
public String solution(String newId) {
// 1단계
newId = newId.toLowerCase();
// 2단계
StringBuilder sb = new StringBuilder();
for(int i = 0; i < newId.length(); i++){
char ch = newId.charAt(i);
if(('a' <= ch && ch <= 'z') || (ch == '-' || ch == '_' || ch == '.') ||
('0' <= ch && ch <= '9')){
sb.append(ch);
}
}
newId = sb.toString();
// 3단계
sb = new StringBuilder();
char prev = 'a';
for(int i = 0; i < newId.length(); i++){
char ch = newId.charAt(i);
if(prev == '.' && ch == '.') continue;
prev = ch;
sb.append(String.valueOf(ch));
}
newId = sb.toString();
// 4단계
if(newId.length() != 0){
if(newId.charAt(0) == '.'){
newId = newId.substring(1, newId.length());
}
}
if(newId.length() != 0){
if(newId.charAt(newId.length() - 1) == '.'){
newId = newId.substring(0, newId.length() - 1);
}
}
// 5단계
if(newId.length() == 0){
newId = "a";
}
// 6단계
if(newId.length() >= 16){
newId = newId.substring(0, 15);
if(newId.charAt(newId.length() - 1) == '.'){
newId = newId.substring(0, newId.length() - 1);
}
}
// 7단계
int count = newId.length();
String last = String.valueOf(newId.charAt(count - 1));
while(count < 3){
newId += last;
count++;
}
return newId;
}
}📌 개인정보 수집 유효기간📖 문제 요약
💡 풀이
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
int currentDay = convertDayFrom(today);
Map<String, Integer> contractDayMap = new HashMap<>();
// 계약 input
for(int i = 0; i < terms.length; i++){
String[] termArr = terms[i].split(" ");
String alphabet = termArr[0];
int month = Integer.parseInt(termArr[1]);
contractDayMap.put(alphabet, month * 28);
}
// 프라이버시
List<Integer> result = new ArrayList<>();
for(int i = 0; i < privacies.length; i++){
String[] privacyArr = privacies[i].split(" ");
int day = convertDayFrom(privacyArr[0]);
String alphabet = privacyArr[1];
int limitDay = day + contractDayMap.get(alphabet);
if(currentDay >= limitDay){
result.add(i + 1);
}
}
return result.stream()
.mapToInt(Integer::intValue)
.toArray();
}
private static int convertDayFrom(String input){
String[] times = input.split("\\.");
int year = Integer.parseInt(times[0]);
int month = Integer.parseInt(times[1]);
int day = Integer.parseInt(times[2]);
return day + month * 28 + year * 12 * 28;
}
} |
0 replies
1️⃣ 신규 아이디 추천function solution(new_id) {
let str = new_id;
// 1. 소문자로 치환
str = str.toLowerCase();
// 2. 알파벳 소문자, 숫자, 빼기(-), 밑줄(_), 마침표(.)만 남기기
let temp = '';
for(let ch of str) {
if((ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') ||
(ch === '-' || ch === '_' || ch === '.')) {
temp += ch;
}
}
str = temp;
// 3. 마침표 2번 이상 연속되면 하나로 치환
temp = '';
for(let i = 0; i < str.length; i++) {
if(!(str[i] === '.' && temp[temp.length - 1] === '.')) {
temp += str[i];
}
}
str = temp;
// 4. 마침표가 처음이나 끝에 있으면 제거
if(str[0] === '.') str = str.slice(1);
if(str[str.length - 1] === '.') str = str.slice(0, -1);
// 5. 빈 문자열이면 'a' 대입
if(str === '') str = 'a';
// 6. 길이가 16자 이상이면 15자까지 자르고, 끝에 마침표 있으면 제거
if(str.length >= 16) str = str.slice(0, 15);
if(str[str.length - 1] === '.') str = str.slice(0, -1);
// 7. 길이가 2자 이하라면, 마지막 문자를 길이가 3 될 때까지 반복
while(str.length <= 2) {
str += str[str.length - 1];
}
return str;
}2️⃣ 개인정보 수집 유효기간function solution(today, terms, privacies) {
// 모든 달 28일
// 날짜 일수로
const days = (date) => {
const [year, month, day] = date.split('.').map(Number);
return year * 12 * 28 + month * 28 + day;
}
// 오늘 날짜 일수로
const todayDays = days(today);
// 파기 대상 개인정보 번호 담을 배열
let result = [];
const termsMap = {};
for(let term of terms) {
const [type, month] = term.split(' ');
termsMap[type] = Number(month);
}
privacies.forEach((privacy, index) => {
const [date, type] = privacy.split(' ');
// 개인 정보 수집 일자 일수로
const collected = days(date);
const expired = collected + termsMap[type] * 28;
if(expired <= todayDays) {
result.push(index + 1);
}
})
return result;
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
📢 이번 주 알고리즘 스터디 문제
이번 주에는 총 4문제를 풉니다.
(정답률은 프로그래머스 기준)
📌 문제 목록 [ 1단계 ]
🗓️ 발표
🚨 벌금 규칙
🔥 이번 주도 화이팅입니다!
All reactions