The goal is to convert a bitString into a plain text using Huffman code table,
r=000
h=001
o=01
w=100
d=1010
e=1011
l=11
I stored the Huffman code table in two different String[] arrays:
String[] ch = {"r", "h", "o", "w", "d", "e", "l"};
String[] b = {"000", "001", "01", "100", "1010", "1011", "11"};
According to the Huffman code table, the following bitString is equivalent to the string "helloworld".
String bits = "001101111110110001000111010";
Now I want to loop through each set of bits in order to match up its corresponding character:
StringBuilder sb = new StringBuilder();
for(int i = 0; i < bits.length(); i++) {
if(bits.substring(0, b[i].length()).equals(b[i])) {
sb.append(ch[i]);
bits = bits.substring(b[i].length());
}
}
The problem here is that each time a match is found, I cannot find the way to "reset" the loop and go back to b[0] so I could check b[i] back from the beginning.