In the following code I am trying to count all the possible binary sub strings of length m within an array of binary numbers, which means there are 2^m possible sub strings that can be found within given binary array.
I have tried accomplishing the task using following approach:
byte [] E = {0,1,0,0,1,1,0,1,0,1,0,1};
int m=3;
int [] c = new int [(int)Math.pow(2,m)];
for(int i=0;i<n;i++)
{
int g=0;
for(int j=0;j<m;j++)
{
g <<= 1;
if(E[i+j]==1)
g++;
}
c[g]++;
}
for(int i=0;i<c.length;i++)
System.out.print("n("+i+")->"+c[i]+" ");
Output:
n(0)->0 n(1)->1 n(2)->3 n(3)->1 n(4)->1 n(5)->3 n(6)->1 n(7)->0
The above approach requires 2^m memory to be allocated to array 'c' which will generate OutOfMemoryError for a large value of m (say m=30).
My questions:
1.Is there any better approach to avoid such error since the value of m might be very large and memory allocation might not be allowed?
2.How can I test accurately, if the memory allocation to the array is possible prior to actual allocation, I have already tried using
if (Runtime.getRuntime().freeMemory() < ((Integer.SIZE/8)* Math.pow(2, m))) throw new Exception("value of m too large");
to check the available memory but it throws exception when m is between 21 and 25 where as the actual allocation takes place (without using above test condition) for m < 25.
Is my approach correct?
You could use a dictionary instead of an array, and allocate the entries lazily. While that has a lot more overhead per entry, you will have far fewer than 2 m entries especially when m
gets big, because there are only n-m+1
substrings of length m
in a string of length n
. So you might have n-m+1
entries (which for even moderate m
is much better than 2 m ), but only if E has a special structure, usually there will be fewer.
It sounds like you're asking a different question from the math posted
If you're trying to get consecutive portions of size 3 (A) from an array of size 6 (B), there are 4 substrings you could possibly get (B - A + 1)
Main array
BBBBBB
Sub arrays
AAABBB
BAAABB
BBAAAB
BBBAAA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.