Skip to content

Feat:Add solution of #193 #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/Programmers/sa11k/lv2_문자열압축/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
static int answer = 0;
public int solution(String s) {
int len = s.length();
answer = len;

for(int i = 1; i<=len/2; i++){
cut(i, s);
}

return answer;
}

static void cut(int len, String s){
String compareA = s.substring(0, len);
String makeS = "";
int comp = 0;
int same = 1;

for(int i = len; i<=s.length()-len; i+=len){
if(compareA.equals(s.substring(i, i+len))){
same++;
}
else{
if(same>1){
makeS += same+"";
}
makeS+=compareA;
compareA = s.substring(i, i+len);
same = 1;
}
}

if(same>1){
makeS += same+"";
}
makeS += compareA;

int div = s.length() % len;

answer = Math.min(answer, makeS.length()+div);
}
}
68 changes: 68 additions & 0 deletions src/Programmers/sa11k/lv2_문자열압축/Solution_오답.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Solution_오답 {
static int answer = 0;
public int solution(String s) {
int len = s.length();
answer = len;

for(int i = 1; i<=len; i++){
cut(i, s);
}

return answer;
}

static void cut(int len, String s){
String compareA = "";
String compareB = "";
String makeS = "";
int idx = 0;
int same = 1;
for(int j = 0; j<len; j++){
compareA += s.charAt(idx++);
}
if(s.length()/len>=2){
while(idx<=s.length()-len){
compareB = "";
for(int j = 0; j<len; j++){
compareB += s.charAt(idx++);
}
// System.out.println(compareB);
if(compareA.equals(compareB)){
if(idx<s.length()){
same++;
}else{
same++;
// if(same>10) same %= same;
makeS += same+"";
}
}else{
if(same!=1){
// if(same>10) same %= same;
makeS += same + compareA;
same = 1;
compareA = "";
}else if(same == 1){
makeS += compareA;
compareA = "";
}
compareA = compareB;
}
}

makeS += compareB;

for(int i = idx; i<s.length(); i++){
makeS += s.charAt(i);
}
}

else{
makeS = s;
}

// System.out.println(len + " : " +makeS);

if(makeS.length()<answer) answer = makeS.length();

}
}