sandy
BAN USER- 0of 0 votes
Answersyou have the string ={ aabb, aafd,acff,aacg,.....} , if i am writing a as first char or first two char or first three char and so on, it should show the all unique combination of words started with the that characters for eg. say if iam writing aa then it show that aabb or aafd
- sandy in India
i have tried using hashmap is it increase my complexity or should i have to use list| Report Duplicate | Flag | PURGE
Samsung Network Engineer Algorithm Java
/**
*
*/
package treeRelated;
/**
* @author sdinodiya
*
*/
public class pathToLeavesSum {
public static int totalSum = 0;
public static void main(String args[]) {
Node root = new Node(1);
Node left1 = new Node(2);
Node right1 = new Node(3);
//assign the value to the left and right node
root.left = left1;
root.right = right1;
Node left2 = new Node(4);
Node right2 = new Node(5);
//assign the values to the left node of the root child
root.left.left = left2;
root.left.right = right2;
Node left3 = new Node(6);
Node right3 = new Node(7);
//assign the values to the right node of the root child
root.right.left = left3;
root.right.right = right3;
System.out.println("sum is " + pathToSum(root , root.data));
}
private static int pathToSum(Node root, int sum) {
if(root.left == null && root.right == null){
System.out.println("Brach Sum:"+sum);
totalSum += sum;
}
if(root.left != null){
pathToSum(root.left, (sum*10) + root.left.data);
}
if(root.right != null){
pathToSum(root.right, (sum*10) + root.right.data);
}
return totalSum ;
}
}
public class Node {
int data;
Node left ;
Node right ;
Node (int data){
this.data = data ;
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class StringPattern {
public static void main(String[] args) {
String s ="abasjnabmzcmskciowecmsncjfgewtcjn";
TreeMap<Character, Integer> hmap = new TreeMap<>();
for(Character c : s.toCharArray()){
Integer freq=hmap.get(c);
if(freq==null){
freq=1;
}
hmap.put(c,freq+1);
}
StringBuffer sb = new StringBuffer();
for(Map.Entry<Character, Integer> m : hmap.entrySet()){
Character c1= m.getKey();
for(int i=0 ,count=hmap.get(c1).intValue();i<count;i++){
sb.append(m.getKey());
}
}
System.out.println(sb);
}
}
- sandy March 17, 2016