-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q4.java
164 lines (146 loc) ยท 5.25 KB
/
Q4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ch01;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 1.4. ํ๋ฌธ ์์ด
*
* #106. ๋ชจ๋ ์์ด์ ์ ๋ถ ์์ฑํ ํ์๊ฐ ์๋ค. ์์ฃผ ๋นํจ์จ์ ์ด๋ค.
* #121. ์ด๋ค ๋ฌธ์์ด์ด ํ๋ฌธ์ ์์ด์ผ ๋, ๊ทธ ํน์ฑ์ ๋ฌด์์ธ๊ฐ?
* #134. ํด์ํ
์ด๋ธ์ ์ฌ์ฉํด ๋ณธ ์ ์ด ์๋๊ฐ? O(N) ์๊ฐ์ผ๋ก ์ค์ผ ์ ์๋ค.
* #136. ๋นํธ ๋ฒกํฐ๋ฅผ ์ฌ์ฉํด์ ๊ณต๊ฐ์ ์ค์ผ ์ ์๋๊ฐ?
*/
public class Q4 {
public static void main(String[] args) {
Q4 q4 = new Q4();
assertTrue(q4.isPalindromePermutation("Tact Coa")); // t: 2, a: 2, c:2, o: 1
}
// Solution 1. Hash Table
// - ํ๋ฌธ์ ์์ด: ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ฝ์ด๋ ๊ฐ์ ๋ฌธ์์ด
// 1) ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ์ง์์ผ ๋: ๋ชจ๋ ๋ฌธ์์ ๊ฐ์๊ฐ ์ง์ ๊ฐ
// 2) ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ํ์์ผ ๋: ๋จ ํ ๊ฐ์ ๋ฌธ์๋ง ํ์ ๊ฐ, ๋๋จธ์ง๋ ์ง์ ๊ฐ
// - time: O(N)
public boolean solution_isPermutationOfPalindrome_hashTable(String phrase) {
int[] table = solution_buildCharFrequencyTable(phrase);
return solution_checkMaxOneOdd(table);
}
/**
* ํ์ ๋ฌธ์๊ฐ ํ ๊ฐ ์ด์ ์กด์ฌํ๋์ง ํ์ธํ๋ค.
*/
private boolean solution_checkMaxOneOdd(int[] table) {
boolean foundOdd = false;
for (int count : table) {
if (count % 2 == 1) {
if (foundOdd) {
return false;
}
foundOdd = true;
}
}
return foundOdd;
}
/**
* ๊ฐ ๋ฌธ์๊ฐ ๋ช ๋ฒ ๋ฑ์ฅํ๋์ง ์ผ๋ค.
*/
private int[] solution_buildCharFrequencyTable(String phrase) {
int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
for (char c : phrase.toCharArray()) {
int x = solution_getCharNumber(c);
if (x != -1) {
table[x]++;
}
}
return table;
}
/**
* ๊ฐ ๋ฌธ์์ ์ซ์๋ฅผ ๋์์ํจ๋ค. (a -> 0, b -> 1, c -> 2 ๋ฑ)
* ๋์๋ฌธ์ ๊ตฌ๋ถ์ด ์๊ณ , ๋ฌธ์๊ฐ ์๋ ๊ฒฝ์ฐ์๋ -1๋ก ๋์์ํจ๋ค.
*/
private int solution_getCharNumber(char c) {
int a = Character.getNumericValue('a');
int z = Character.getNumericValue('z');
int val = Character.getNumericValue(c);
if (a <= val && val <= z) {
return val - a;
}
return -1;
}
// Solution 2. ์ํ ํ ๋ฒ์ผ๋ก ๊ฐ์ (big-O๋ ๋์ผ)
// - ๋ง์ง๋ง์ ๊ฐ์ ํ์ ๊ฐ์ธ์ง ํ์ธํ๊ธฐ๋ณด๋จ ๋ฌธ์์ด์ ํ์ด ๋๊ฐ๋ฉด์ ๋์์ ํ์์ ๊ฐ์๋ฅผ ํ์ธํ ์๋ ์๋ค.
public boolean isPermutationOfPalindrome_optimized(String phrase) {
int countOdd = 0;
int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
for (char c : phrase.toCharArray()) {
int x = solution_getCharNumber(c);
if (x != -1) {
table[x]++;
if (table[x] % 2 == 1) {
countOdd++;
} else {
countOdd--;
}
}
}
return countOdd <= 1;
}
// Solution 3. ๋นํธ ๋ฒกํฐ
public boolean isPermutationOfPalindrome_bit(String phrase) {
int bitVector = solution_createBitVector(phrase);
return bitVector == 0 || soltuion_checkExactlyOneBitSet(bitVector);
}
/**
* ๋ฌธ์์ด์ ๋ํ ๋นํธ ๋ฒกํฐ๋ฅผ ๋ง๋ ๋ค.
* ๊ฐ์ด i์ธ ๋ฌธ์๊ฐ ๋ฑ์ฅํ๋ฉด i๋ฒ์งธ ๋นํธ๊ฐ์ ๋ฐ๊พผ๋ค.
*/
private int solution_createBitVector(String phrase) {
int bitVector = 0;
for (char c : phrase.toCharArray()) {
int x = solution_getCharNumber(c);
bitVector = solution_toggle(bitVector, x);
}
return bitVector;
}
/**
* ์ ์์ i๋ฒ์งธ ๋นํธ๊ฐ์ ๋ฐ๊พผ๋ค.
*/
private int solution_toggle(int bitVector, int index) {
if (index < 0) {
return bitVector;
}
int mask = 1 << index;
if ((bitVector & mask) == 0) {
bitVector |= mask;
} else {
bitVector &= ~mask;
}
return bitVector;
}
/**
* ์ ํํ ๋นํธ ํ ๊ฐ๋ง 1๋ก ์ธํ
๋๋์ง ํ์ธํ๊ธฐ ์ํด ์ฃผ์ด์ง ์ ์๊ฐ์์ 1์ ๋บ ๋ค
* ์๋ ๊ฐ๊ณผ AND ์ฐ์ฐ์ ํ๋ค.
*/
private boolean soltuion_checkExactlyOneBitSet(int bitVector) {
return (bitVector & (bitVector - 1)) == 0;
}
// Method 1. HashMap(Hash Table)
public boolean isPalindromePermutation(String str) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isAlphabetic(ch)) {
ch = Character.toLowerCase(ch);
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
}
boolean doesExistOddCount = false;
for (Character ch : map.keySet()) {
if (map.get(ch) % 2 == 1) {
if (doesExistOddCount) {
return false;
}
doesExistOddCount = true;
}
}
return true;
}
}