Rahul Khanna
BAN USERMy solution :-
public static void main(String[] args) {
int sum = getSum(10);
System.out.println("Sum = "+sum);
}
private static int getSum(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
return sum;
}
}
My solution :-
public List<String> doBashExpansion(List<String> input) {
List<String> prefixCombi = new ArrayList<>();
String midChar = "";
String lastChar = "";
for (String toProcess : input) {
List<String> combi = null;
String[] chs = toProcess.split(",");
List<String> prefix = new ArrayList<>();
for (String ch : chs) {
if (ch.contains(")")) {
int index = ch.indexOf(')');
prefix.add(ch.substring(0, index));
midChar = ch.substring(index + 1, ch.length());
} else if (ch.startsWith("(")) {
prefix.add(ch.replace('(', (char) 0));
} else{
lastChar = ch;
}
}
combi = populateCombination(prefix, midChar);
prefixCombi.addAll(combi);
}
List<String> finalParsing = doFinalParsing(prefixCombi);
finalParsing.add(lastChar);
finalParsing.remove(",");
return finalParsing;
}
private List<String> doFinalParsing(List<String> prefixCombi) {
int indexForSplit = prefixCombi.indexOf(",");
List<String> output = new ArrayList<String>();
List<String> first = prefixCombi.subList(0, indexForSplit);
List<String> last = prefixCombi.subList(indexForSplit,
prefixCombi.size());
for (String postFix : last) {
if (postFix.equals(",")) {
continue;
}
output.addAll(populateCombination(first, postFix));
}
output.remove(",");
return output;
}
private List<String> populateCombination(List<String> prefix, String mid) {
List<String> prefixCombo = new ArrayList<>();
for (String pre : prefix) {
pre = pre + mid;
prefixCombo.add(pre);
}
prefixCombo.add(",");
return prefixCombo;
}
/**
* @param args
*/
public static void main(String[] args) {
Question1 ans = new Question1();
String regExp = "[(][a-z]+,[a-z]+[)][a-z,]*";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(args[0]);
List<String> input = new ArrayList<>();
while (matcher.find()) {
input.add(matcher.group());
}
System.out.println(ans.doBashExpansion(input));
}
The below function solves the problem..
public static int getSumNoLeaf(BTNode root){
int sum = 0;
BTNode temp = root;
Stack<BTNode> s = new Stack<BTNode>();
s.push(root);
while(temp != null){
if(s.isEmpty())
break;
temp = s.pop();
if(temp.getLeft()!= null || temp.getRight()!=null)
sum = sum+ temp.getData();
if(temp.getLeft()!= null){
s.push(temp.getLeft());
if(temp.getRight()!= null)
s.push(temp.getRight());
}
}
return sum;
}
I believe below function will give the desired result in O(n). Please suggest if any improvement can be done in below code.
public static int countBlocks(DLLNode head, DLLNode[] input){
HashSet<DLLNode> s = new HashSet<DLLNode>(Arrays.asList(input));
DLLNode temp = head;
int blockCount = 0;
while(temp != null){
if(temp.getLeft()== null && s.contains(temp))
blockCount++;
if(temp.getLeft() !=null && s.contains(temp) && !s.contains(temp.getLeft()))
blockCount++;
temp = temp.getRight();
}
return blockCount;
}
I think below function should work fine..and its run time complexity is O(n)
public static List<String> getConsecutiveRepeatedStrings(String[] input){
int inputLen = input.length;
List<String> li = new ArrayList<String>();
int strPtr = 0, i = 0 , j = 1;
while(strPtr<inputLen){
if(input[strPtr].charAt(i) == input[strPtr].charAt(j)){
li.add(input[strPtr]);
strPtr++;
i = 0 ;
j = 1;
}
else if(j < input[strPtr].length()-1){
i++;
j++;
}
else{
strPtr++;
i = 0 ;
j = 1;
}
}
return li;
}
I think this code should solve this problem..
public static String eliminate(String data){
char[] strArr = data.toCharArray();
int len = strArr.length;
int i = 0 ;
while(len>0){
if(strArr[i]=='b')
strArr[i]= ' ';
if( i<data.length() && strArr[i]== 'a' && strArr[i+1]=='c')
strArr[i] = strArr[i+1] = ' ';
i++;
len--;
}
return new String(strArr);
}
My solution would be like this..
- Rahul Khanna February 15, 2015