Skip to content

Instantly share code, notes, and snippets.

@keehyun2
Created February 7, 2020 01:20
Show Gist options
  • Save keehyun2/06aafcce7b708c0ddcd72669f700c5c4 to your computer and use it in GitHub Desktop.
Save keehyun2/06aafcce7b708c0ddcd72669f700c5c4 to your computer and use it in GitHub Desktop.
package programers;
import java.util.HashMap;
import java.util.Map;
public class StringZip {
public int solution2(String s) {
int minResult = s.length();
for (int unit = 1; unit <= s.length() / 2; unit++) { // for #01
int resultLength = unit;
int repeatCnt = 1;
String preStr = s.substring(0 , unit);
String postStr;
for (int i = 0; i < s.length()/unit - 1 ; i++) { // for #02
postStr = s.substring((i + 1) * unit , (i + 2)*unit);
if(preStr.equals(postStr)) {
// i 위치에서 다음문자와 일치하는 경우
// 숫자를 붙히고 길이가 + 1 됨, 직전 위치에도 일치하는 경우였으면 +1 안함.
// 반복되는 갯수가 10개 이상이면 길이가 + 2, 100개 이상이면 숫자 길이가 + 3, 1000개 이상이면 숫자 길이가 + 4 가 됨.
repeatCnt = repeatCnt+1;
// System.out.println( s.substring(i*unit , i*unit + unit) +" "+repeatCnt);
if(repeatCnt == 2 || repeatCnt == 10 || repeatCnt == 100 || repeatCnt == 1000) {
// System.out.println(i + ", " + s.substring(i*unit , i*unit + unit));
resultLength = resultLength + 1;
}
}else {
// i 위치에서 다음문자와 일치하지 않는 경우
// 새 문자 추가되고 길이 + 1됨.
resultLength = resultLength + unit;
repeatCnt = 1;
}
preStr = postStr;
}
resultLength = resultLength + s.length() % unit; // 문자열을 단위로 나눈 나머지를 더함.
if(minResult > resultLength) {
minResult = resultLength;
}
}
return minResult;
}
public int solution(String s) {
int answer = 0;
for(int i=1; i<=(s.length()/2)+1; i++){
int result = getSplitedLength(s, i, 1).length();
answer = i==1 ? result : (answer>result?result:answer);
}
return answer;
}
/**
*
* @param s 문자열
* @param n 단위
* @param repeat 반복된 횟수
* @return
*/
public String getSplitedLength(String s, int n, int repeat){
if(s.length() < n) return s; // 기저 조건
String result = "";
String preString = s.substring(0, n);
String postString = s.substring(n, s.length());
// 불일치 -> 현재까지 [반복횟수 + 반복문자] 조합
if(!postString.startsWith(preString)){
if(repeat ==1) return result += preString + getSplitedLength(postString, n, 1);
return result += Integer.toString(repeat) + preString + getSplitedLength(postString, n, 1);
}
return result += getSplitedLength(postString, n, repeat+1);
}
public static void main(String[] args) {
// System.out.println(new StringZip().solution("aabbaccc")); // 7
// System.out.println(new StringZip().solution("ababcdcdababcdcd")); // 9
// System.out.println(new StringZip().solution("abcabcdede")); // 8
// System.out.println(new StringZip().solution("abcabcabcabcdededededede")); // 14
// System.out.println(new StringZip().solution("xababcdcdababcdc")); // 16
// System.out.println(new StringZip().solution("aaaaaaaaaa"));
// System.out.println(new StringZip().getSplitedLength("aaaabbaccc", 2, 1));
System.out.println(new StringZip().numJewelsInStones("ab","aaaabbaccc"));
}
// https://leetcode.com/problems/jewels-and-stones
public int numJewelsInStones(String J, String S) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char key : S.toCharArray()) {
map.put(key, map.getOrDefault(key, 0) + 1);
}
int result = 0;
for (char key : J.toCharArray()) {
result = result + map.getOrDefault(key, 0);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment