0% found this document useful (0 votes)
2 views

Binary Decimal Count

Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary Decimal Count

Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

public static int getCount(String binary){

findsubsequences(binary, "");
System.out.println("set size "+set.size());
return set.size();
}

private static void findsubsequences(String s,


String ans)
{
if (s.length() == 0) {
al.add(ans);
convertToDecimal(ans);
return;
}
findsubsequences(s.substring(1), ans + s.charAt(0));
findsubsequences(s.substring(1), ans);
}

private static void convertToDecimal(String s){


s = removeLeadingZeros(s);
set.add(0);
System.out.println(s);
if(s.length()!=0){
int num = Integer.valueOf(s);
int dec_value = 0;
int base = 1;

int temp = num;


while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;

dec_value += last_digit * base;

base = base * 2;
}

set.add(dec_value);
}
}

private static String removeLeadingZeros(String str){


int i = 0;
while (i < str.length() && str.charAt(i) == '0')
i++;

StringBuffer sb = new StringBuffer(str);

sb.replace(0, i, "");
return sb.toString();
}

You might also like