-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStringWithXOR.java
More file actions
35 lines (27 loc) · 950 Bytes
/
Copy pathReverseStringWithXOR.java
File metadata and controls
35 lines (27 loc) · 950 Bytes
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
package algorithms.numbers.bits;
import static algorithms.Assert.assertEquals;
/**
* Reverse String using XOR.
* Source: https://habr.com/post/183462
*/
public class ReverseStringWithXOR {
public static String reverseWithXOR(String string) {
char[] c = string.toCharArray();
int n = c.length;
int mid = (int) Math.floor(n / 2);
for (int i = 0; i < mid; i++) {
c[i] ^= c[n - i - 1];
c[n - i - 1] ^= c[i];
c[i] ^= c[n - i - 1];
}
return new String(c);
}
public static void main(String[] args) {
assertEquals(reverseWithXOR("a"), "a");
assertEquals(reverseWithXOR("ab"), "ba");
assertEquals(reverseWithXOR("abcd"), "dcba");
assertEquals(reverseWithXOR("abcde"), "edcba");
assertEquals(reverseWithXOR("1a2b3c"), "c3b2a1");
assertEquals(reverseWithXOR("1a2b3c4"), "4c3b2a1");
}
}