Skip to content

Commit 2829136

Browse files
committed
Add Algorithm
1 parent 2db27c0 commit 2829136

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version '1.0-SNAPSHOT'
33

44
apply plugin: 'java'
55

6-
sourceCompatibility = 1.5
6+
sourceCompatibility = 1.8
77

88
repositories {
99
mavenCentral()

src/test/java/algorithm/CharacterCompressWithLength.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,63 @@
22

33
import org.junit.Test;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
import static org.hamcrest.CoreMatchers.is;
69
import static org.junit.Assert.assertThat;
710

811
public class CharacterCompressWithLength {
912

13+
/*
14+
TASK
15+
주어진 문자열을 길이와 함께 적어주면서 압축을 한다.
16+
*/
17+
1018
@Test
1119
public void test() {
12-
assertThat("", is(testFunction()));
20+
assertThat(null, is(RunLengthCompress_USE_HASHMAP(null)));
21+
assertThat("a3b3c3", is(RunLengthCompress_USE_HASHMAP("aaabbbccc")));
22+
assertThat("a3b3c4", is(RunLengthCompress_USE_HASHMAP("aabbacbccc")));
23+
24+
assertThat("a3b3c3", is(RunLengthCompress("aaabbbccc")));
1325
}
1426

15-
public String testFunction() {
16-
return "";
27+
private String RunLengthCompress_USE_HASHMAP(String str) {
28+
if (str == null) return null;
29+
30+
Map<Character, Integer> charCounter = new HashMap<>(str.length());
31+
32+
for (char c : str.toCharArray()) {
33+
if (charCounter.containsKey(c)) {
34+
charCounter.put(c, charCounter.get(c) + 1);
35+
} else {
36+
charCounter.put(c, 1);
37+
}
38+
}
39+
StringBuilder sb = new StringBuilder();
40+
charCounter.forEach((chr, count) -> sb.append(chr + count.toString()));
41+
return sb.toString();
42+
}
43+
44+
private String RunLengthCompress(String str) {
45+
if (str == null) return null;
46+
47+
char[] ca = str.toCharArray();
48+
String result = "";
49+
int count = 1;
50+
char prev = ca[0];
51+
52+
for (int i = 1; i < ca.length; i++) {
53+
if (prev == ca[i]) {
54+
count++;
55+
} else {
56+
result = result + prev + count;
57+
count = 1;
58+
prev = ca[i];
59+
}
60+
}
61+
result = result + prev + count;
62+
return result;
1763
}
1864
}

src/test/java/algorithm/IsAnagram.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void test() {
3030
private boolean 애너그램판별_USE_MAP(String str1, String str2) {
3131
if (str1.length() != str2.length()) return false;
3232

33-
Map<Character, Integer> strMap = new HashMap<Character, Integer>();
33+
Map<Character, Integer> strMap = new HashMap<>();
3434

3535
for (char c : str1.toCharArray()) {
3636
if (strMap.containsKey(c)) {

src/test/java/algorithm/UniqueCharacterInString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void test() {
3939
}
4040

4141
private boolean HashSet을_사용한_방법(String str) {
42-
Set<Character> strSet = new HashSet<Character>();
42+
Set<Character> strSet = new HashSet<>();
4343
for (char c : str.toCharArray()) {
4444
if (strSet.contains(c)) {
4545
return false;

0 commit comments

Comments
 (0)