Skip to content

Commit 2697de1

Browse files
committed
Add Algorithm
1 parent 47ea99a commit 2697de1

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package algorithm;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class CharacterCompressWithLength {
9+
10+
@Test
11+
public void test() {
12+
assertThat("", is(testFunction()));
13+
}
14+
15+
public String testFunction() {
16+
return "";
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package algorithm;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class IsAnagram {
12+
13+
/*
14+
TASK
15+
주어진 문자열이 애너그램인지를 판단한다.
16+
*/
17+
18+
// 방법 1. O(nlogn) : 정렬 후에 비교하기
19+
// 방법 2. O(n) : 문자열 개수를 세서 비교하기
20+
21+
@Test
22+
public void 애너그램판별하기() {
23+
assertThat(true, is(애너그램판별_USE_MAP("arc", "car")));
24+
assertThat(true, is(애너그램판별_USE_MAP("caaabbb", "abababc")));
25+
assertThat(false, is(애너그램판별_USE_MAP("caabbbb", "abababc")));
26+
assertThat(false, is(애너그램판별_USE_MAP("arc", "carr")));
27+
assertThat(false, is(애너그램판별_USE_MAP("arc", "caz")));
28+
}
29+
30+
private boolean 애너그램판별_USE_MAP(String str1, String str2) {
31+
if (str1.length() != str2.length()) return false;
32+
33+
Map<Character, Integer> strMap = new HashMap<Character, Integer>();
34+
35+
for (char c : str1.toCharArray()) {
36+
if (strMap.containsKey(c)) {
37+
strMap.put(c, strMap.get(c) + 1);
38+
} else {
39+
strMap.put(c, 1);
40+
}
41+
}
42+
43+
for (char c : str2.toCharArray()) {
44+
if (!strMap.containsKey(c)) {
45+
return false;
46+
}
47+
if (strMap.get(c) == 0) {
48+
return false;
49+
}
50+
strMap.put(c, strMap.get(c) - 1);
51+
}
52+
53+
return true;
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package algorithm;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class StringParseToInt {
9+
10+
/*
11+
TASK
12+
주어진 문자열을 int 형으로 변환한다.
13+
*/
14+
15+
@Test
16+
public void toIntFromString() {
17+
assertThat(123, is(toIntFromString("123")));
18+
}
19+
20+
private int toIntFromString(String str) {
21+
// return Integer.parseInt(str);
22+
char[] strArr = str.toCharArray();
23+
int base = 0;
24+
for (char c : strArr) {
25+
base *= 10;
26+
base += c - '0';
27+
}
28+
return base;
29+
}
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package algorithm;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class UniqueCharacterInString {
12+
13+
/*
14+
TASK
15+
주어진 문자열에서 문자열을 구성하고 있는 각각의 문자열들이 고유한지를 판단한다.
16+
*/
17+
18+
@Test
19+
public void 문자열내에서_각문자열이_고유한가() {
20+
assertThat(true, is(이중포문을_사용한_방법("abcd")));
21+
assertThat(true, is(이중포문을_사용한_방법("abcdefghij")));
22+
assertThat(false, is(이중포문을_사용한_방법("abccde")));
23+
assertThat(false, is(이중포문을_사용한_방법("abca")));
24+
25+
assertThat(true, is(HashSet을_사용한_방법("abcd")));
26+
assertThat(true, is(HashSet을_사용한_방법("abcdefghij")));
27+
assertThat(false, is(HashSet을_사용한_방법("abccde")));
28+
assertThat(false, is(HashSet을_사용한_방법("abca")));
29+
}
30+
31+
private boolean 이중포문을_사용한_방법(String str) {
32+
char[] strArr = str.toCharArray();
33+
for (int i = 0; i < strArr.length - 1; i++) {
34+
for (int j = i + 1; j < strArr.length; j++) {
35+
if (strArr[i] == strArr[j]) return false;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
private boolean HashSet을_사용한_방법(String str) {
42+
Set<Character> strSet = new HashSet<Character>();
43+
for (char c : str.toCharArray()) {
44+
if (strSet.contains(c)) {
45+
return false;
46+
}
47+
strSet.add(c);
48+
}
49+
return true;
50+
}
51+
}

0 commit comments

Comments
 (0)