anonymousNg
BAN USER
public void removeNonDup(String s1, String s2){
if(s1==null || s2 == null){
System.out.println("string cant be null");
}
else{
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
StringBuffer sb = new StringBuffer();
HashSet<Character> m = new HashSet<Character>();
for(char ch : c1){
m.add(ch);
}
for(char ss : c2){
if(m.contains(ss)){
sb = sb.append(ss);
}
}
System.out.println(sb);
}
}
public void removeNonDup(String s1, String s2){
if(s1==null || s2 == null){
System.out.println("string cant be null");
}
else{
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
StringBuffer sb = new StringBuffer();
HashSet<Character> m = new HashSet<Character>();
for(char ch : c1){
m.add(ch);
}
for(char ss : c2){
if(m.contains(ss)){
sb = sb.append(ss);
}
}
System.out.println(sb);
}
}
public static void main(String args[]){
RemoveNonDupCharBetween2Strings r = new RemoveNonDupCharBetween2Strings();
r.removeNonDup("string", "strong");
r.removeNonDup("", "");
r.removeNonDup(" ", " ");
r.removeNonDup(null, null);
r.removeNonDup(null, "1234");
r.removeNonDup("12346", "12345");
r.removeNonDup("sstring", "sstrong");
r.removeNonDup("sstrign", "sstrgno");
}
private static boolean recArray ( int[] nums, int index, int sum1, int sum2 ) {
if ( index >= nums.length ) {
return sum1 == sum2;
}
int value = nums[index];
return (recArray(nums, index + 1, sum1 + value, sum2) || recArray(nums, index + 1, sum1, sum2 + value));
}
where index =0 at the beginning
- anonymousNg November 12, 2017public void getCharThatOccuredMaxTimes(String s){
char[] carr = s.toCharArray();
LinkedHashMap<Character,Integer> lmap = new LinkedHashMap<>();
for(Character c : carr){
if(lmap.containsKey(c)){
lmap.put(c, lmap.get(c)+1);
}
else{
lmap.put(c, 1);
}
}
int max =0;
char ch = 0;
for(Entry<Character, Integer> entry : lmap.entrySet()){
if(max < entry.getValue()){
max = entry.getValue();
ch = entry.getKey();
}
}
System.out.println(ch +""+ max);
}
public void reverseArrN(int[] a, int n){
int len = a.length; //9-3=6
//int k = n-1;
if(a.length==0){
System.out.println("empty array");
}
else if(a.length==1){
System.out.println("Array has only 1 element");
}
else if(a.length<n){
System.out.println("reverse entire array or invalid usecase a n cannot be greater than length of array.");
}
else{
for(int i=0;i<len;i+=n-1){
int k = i + (n-1);
while(i<k && k<len){
int temp = a[i];
a[i] = a[k];
a[k] = temp;
k--;i++;
}
}
}
}
public void getCount(String s){
char[] carr = s.toCharArray();
LinkedHashMap<Character,Integer> lmap = new LinkedHashMap<>();
for(Character c : carr){
if(lmap.containsKey(c)){
lmap.put(c, lmap.get(c)+1);
}
else{
lmap.put(c, 1);
}
}
for(Entry<Character, Integer> entry : lmap.entrySet()){
System.out.print(entry.getKey()+""+entry.getValue());
}
}
wc -l *
- anonymousNg September 03, 2017public LinkedListNode nthToLastLessComplexity(int n) { //is the value of nth node from last
if(head== null){
return null;
}
LinkedListNode currentNode = head;
int length = 0;
while(currentNode!=null){
currentNode = currentNode.getNext();
length ++;
}
int m = length;
int val = m-n+1;
currentNode = head;
for(int i=1;i<val;i++){
currentNode = currentNode.getNext();
}
return currentNode;
//complexity is O(n)
}
product Ai....Aj
/*Apple Map Team
1. Given an array A and some queries, query(i, j) returns the result of Ai*...*Aj, in other words the multiplication from Ai to Aj.
The numbers in A are non-negative.
Implement query(i, j).
*/
public class RetrunResultOfAiAj {
public static int findProd(int i,int j, int[] a){
if(a==null){
return 0;
}
if(i>a.length-1 || j>a.length-1 || i<0 || j<0){
return 0;
}
// if(i==j){
// return a[i]*a[j];
// }
int m = j-i+1;
int product = 1;
if(m>=1){
product = a[i] * findProd(i+1,j,a);
}
return product;
}
public static void main(String[] args){
int a[] = {1,2,3,4,5,6,7};
int b[] = {1,2,3,0,5,6,7};
int c[] = {1};
int d[] = {};
int e[] = {1,1,1,1,1,1,0};
System.out.println(RetrunResultOfAiAj.findProd(1, 4, a));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, b));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, c));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, d));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, e));
System.out.println(RetrunResultOfAiAj.findProd(1, 14, b));
// System.out.println(RetrunResultOfAiAj.findProd(1, 1, b)); wont work if i and j is same.
}
}
package com.prep.tree;
public class BinaryTreeNotationABCQuestion {
Node root;
BinaryTreeNotationABCQuestion()
{
root = null;
}
public void insertRec(int data){
root = insertInTree(root,data);
}
public Node insertInTree(Node root,int data){
if (root == null){
root = new Node(data);
return root;
}
if(data<root.key){
root.left = insertInTree(root.left,data);
}
else if(data>root.key){
root.right = insertInTree(root.right,data);
}
return root;
}
public String findRec(int data){
String a = findAnotations(root,data);
return a;
}
public String findAnotations(Node root,int value){
//return 0 for left
// return 1 for right
//retrun Not Found for item not Found
// return Undefined is root is null
String annotation = "";
if(root==null){
//System.out.println("NotFound");
annotation = "NotFound";
return annotation;
}
if(root.key==value){
return annotation;
}
if(value<root.key){
annotation = annotation+"0";
String a = findAnotations(root.left,value);
if(a!="NotFound"){
annotation = annotation + a;
}
else {
annotation = a;
}
}
if(value>root.key){
annotation = annotation+"1";
String a = findAnotations(root.right,value);
if(a!="NotFound"){
annotation = annotation + a;
}
else {
annotation = a;
}
}
return annotation;
}
public static void main(String[] args)
{
BinaryTreeNotationABCQuestion tree = new BinaryTreeNotationABCQuestion();
// tree.root = new Node(100);
// tree.root.left = new Node(60);
// tree.root.right = new Node(150);
// tree.root.left.left = new Node(45);
// tree.root.left.right = new Node(75);
tree.insertRec(5);
tree.insertRec(2);
tree.insertRec(8);
tree.insertRec(3);
tree.insertRec(6);
tree.insertRec(9);
tree.insertRec(1);
String c = tree.findRec(1);
String d = tree.findRec(6);
String e = tree.findRec(10);
String f = tree.findRec(2);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
}
}
public boolean isPerfectSquare(int n) {
int sum = 0;
if(n == 0 || n<0) {
return false;
}
else if(n ==1){
return true;
}
else {
for(int i=1;i<n;i+=2){
sum = sum+i;
if(sum == n)
{
return true;
}
}
return false;
}
}
You can use getClass method to determine the class name. Or use reflection.
- anonymousNg March 30, 2017
- anonymousNg November 12, 2017