-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-3.java
43 lines (43 loc) · 1.39 KB
/
1-3.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
package cc150.chapter1;
class Test1_3 {
public static void main(String args[]) {
String str = "abccdfb";
str = deduplicate(str);
System.out.println(str);
}
public static String deduplicate(String str) {
char[] strchar = str.toCharArray();
int uppermap = 0;
int lowermap = 0;
int len = strchar.length;
for( int i = 0; i < len; i++) {
char c = strchar[i];
if( c >= 'a' && c <= 'z' ) { // Lower case
int mask = 1 << (c - 'a');
if( (lowermap & mask) != 0 ) {
// Move one step left the following chars
for( int j = i; j < len-1; j++ ) {
strchar[j] = strchar[j+1];
}
len--;
i--;
} else {
lowermap |= mask;
}
}
else if( c >= 'A' && c <= 'Z' ) { // Upper case
int mask = 1 << ( c - 'A' );
if( (uppermap & mask) != 0 ) {
for( int j = i; j < len-1; j++ ) {
strchar[j] = strchar[j+1];
}
len--;
i--;
} else {
uppermap |= mask;
}
}
}
return String.valueOf(strchar, 0, len);
}
}