kmlsharma53
BAN USERpublic class TestFibo {
public static void main(String[] args) {
int n =10;
List<String> l = new ArrayList<String>();
for(int j =0;j<=n;j++){
if(genarateFibinacci(j)%2 == 0)
l.add("#");
else
l.add(String.valueOf(genarateFibinacci(j)));
}
for(String s : l){
System.out.println(s);
}
}
static int genarateFibinacci(int n){
if(n==0)
return 0;
else if(n==1)
return 1;
else
return genarateFibinacci(n-1) + genarateFibinacci(n-2);
}
}
public class CharacterReplacer {
public static void main(String[] args) {
String s = "a?b?c?djjjjh?";
List<Integer> list = getNumberOfPosition(s);
String[] str = generateCombination(s,list);
for(String st : str){
System.out.println(st);
}
}
private static String[] generateCombination(String s, List<Integer> list) {
String[] binaryCombination = getBinaryString(list);
String[] allString = new String[binaryCombination.length];
int index = 0;
for(String string : binaryCombination){
StringBuffer stringBuffer = new StringBuffer(s);
for(int k=0;k<list.size();k++){
stringBuffer.replace(list.get(k), list.get(k)+1, string.substring(k,k+1));
}
allString[index++] = stringBuffer.toString();
}
return allString;
}
private static String[] getBinaryString(List<Integer> list) {
int totalCombination = (int) Math.pow(2, list.size());
String[] str = new String[totalCombination];
for(int i = 0 ; i < totalCombination;i++){
str[i] = Integer.toBinaryString(i);
int diff = list.size() - str[i].length();
String s = "";
for(int j=0 ; j<diff ; j++)
s =s + "0";
str[i] = s + str[i];
}
return str;
}
private static List<Integer> getNumberOfPosition(String s) {
char[] ch = s.toCharArray();
List<Integer> list = new ArrayList<>();
for(int i = 0;i < ch.length;i++){
if(ch[i] == '?'){
list.add(i);
}
}
return list;
}
}
private static boolean verifyAnagramExistence(String[] str) {
boolean isAnagramExist = false;
int[] ascii = new int[str.length];
int index = 0;
for(String s : str){
int value = getAsciiValue(s);
if(!isAlreadyExist(ascii,value))
ascii[index++] = value;
else{
isAnagramExist = true;
break;
}
}
return isAnagramExist;
}
private static int getAsciiValue(String s) {
int sum = 0;
for(char ch : s.toCharArray()){
sum += String.valueOf(ch).codePointAt(0);
}
return sum;
}
private static boolean isAlreadyExist(int[] ascii, int value) {
for(int number : ascii){
if(number == value)
return true;
}
return false;
}
complexity is O(n)
- kmlsharma53 June 22, 2014private static String[] removeDupliate(String[] str) {
List<String> list = new ArrayList<String>();
for(String d : str){
if(!list.contains(d))
list.add(d);
}
String[] result= new String[list.size()];
int k=0;
for(String st : list){
result[k++] = st;
}
return result;
}
The complexity is O(n).
- kmlsharma53 June 22, 2014public class BinaryConverter {
public static void main(String[] args) {
String str1 = "1010101010";
String str2 = "0110";
int number1 = getIntegerFromBinary(str1);
int number2 = getIntegerFromBinary(str2);
System.out.println("The sum is :: "+ (number1+number2));
}
private static int getIntegerFromBinary(String str) {
char[] arr = str.toCharArray();
int sum = 0;
for(int i = arr.length-1,j=0;i>=0;i--,j++){
if(arr[i] == '1')
sum = (int) (sum + Math.pow(2, j));
}
return sum;
}
}
public static void main(String args) {
String str = "why so serious";
String[] arr = str.split(" ");
StringBuffer stringBuffer = new StringBuffer();
for(String st : arr){
stringBuffer.append(reverseString(st)+ " ");
}
System.out.println(stringBuffer.toString().trim());
}
private static StringBuffer reverseString(String str) {
StringBuffer buffer = new StringBuffer(str);
return buffer.reverse();
}
public class TrailingZero {
public static void main(String[] args) {
int a = 15;
System.out.println(trailingZero(getFactorial(a)));
}
private static int getFactorial(int a) {
if(a==1)
return 1;
return a*getFactorial(a-1);
}
private static int trailingZero(int a) {
int count = 0;
char[] ch = new Integer(a).toString().toCharArray();
for(int i=ch.length-1;i>=0;i--){
if(ch[i] == '0'){
count++;
}
else{
break;
}
}
return count;
}
}
- kmlsharma53 July 08, 2014