-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringUtils.java
76 lines (69 loc) · 1.88 KB
/
StringUtils.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
public class StringUtils {
/**
* recursive solution
* @param str
* @return
*/
public static String reverse(String str){
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
/**
* using XOR
*
*
<pre>
XOR (Exclusive OR) table
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
First operation: x1 = x1 XOR x2
x1: 1 0 0
x2: 1 1 1
New x1: 0 1 1
Second operation x2 = x2 XOR x1
x1: 0 1 1
x2: 1 1 1
New x2: 1 0 0
Third operation: x1 = x1 XOR x2
x1: 0 1 1
x2: 1 0 0
New x1: 1 1 1
</pre>
And voila, we now have x1 = 111 and x2 = 100.
and for each pair in array we do this because string chars are in essence binary values :)))
This method is also the quickest way to reverse a string.
Recursive way is almost 4-5 times slower.
* @param str : for instance (abcde)
* @return reverse result (edcba)
*/
public static String reverseStrUsingXOR(String str){
// convert the string to char array
char[] charArray = str.toCharArray();
int len = str.length() - 1;
/*
now this for is a bit unconventional at first glance because there
are 2 variables that we're changing values of: i++ and len--.
the order of them is irrelevant. so basicaly we're going with i from
start to end of the array. with len we're shortening the array by one
each time. this is probably understandable.
*/
for (int i = 0; i < len; i++, len--)
{
/*
now this is the tricky part people that should know about it don't.
look at the table below to see what's going on exactly here.
*/
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}
return new String(charArray);
}
public static void main(String[] args) {
//System.out.println(StringUtils.reverseStrUsingXOR("abcde"));
}
}