Muhammad Tariq Akram
BAN USERI fix this in my solution........! in my solution you can provide any string ??? or 1000?, or ????1010??? ....!
- Muhammad Tariq Akram August 21, 2013Argument Can be any patteren with question mark "???" or ?10101?? or 0101010??
public static void genrateStringFromPatteren(String strPattern){
char[] charArray = strPattern.toCharArray();
// No of Wild Card into String.
int intNoWild = strPattern.length() - strPattern.replaceAll("\\?", "").length();
// Power Function will run loop till possible unique combinations.
for (int i = 0; i < Math.pow(2, intNoWild); i++) {
// Get Binary for the number.
String strCombination= this.getCombination(intNoWild, i);
// This will get Value from Combination String to eplace in Array.
int intCounter=0;
for(int k=0; k<charArray.length; k++){
/* If array Location has Wildcard value then get combination
from the combination String and replace with this location.
*/
if (charArray[k]=='?'){
charArray[k] = strCombination.charAt(intCounter);
/* This Counter Will keep track of the next value to be replaced.*/
intCounter++;
}
}
String output =new String(charArray);
System.out.println(output);
// Reset Array to Populate the new combination.
charArray = strPattern.toCharArray();
}
}
public static String getCombination(int valFormat, int NumberValue){
String strCombination = Integer.toBinaryString(NumberValue);
if ( strCombination.length()-1 < valFormat){
for ( int i=strCombination.length(); i<valFormat; i++){
strCombination = "0" + strCombination;
}
}
return strCombination;
}
I just tried differently.....!
- Muhammad Tariq Akram August 20, 2013
I dont understand your question.....!
- Muhammad Tariq Akram August 22, 2013Either it should be out when elements is <3 or How you can delete the 3rd element when there will be only 1 remaining in list. It will throw index out of bound when it try to access 3rd element with 2 remaining in list.