Tuesday, December 4, 2018

Find an element in a two dimensional array in O(n)

#include <iostream>
using namespace std;

int search(int a[4][4], int n, int x){
int i = 0;
int j = n - 1;

while( i < n && j >= 0){
if(a[i][j] == x){
cout<<"Found at "<<i<<" "<<j;
return 1;
} else if(a[i][j] > x){
j--;
} else {
i++;
}
}
}

int main()
{
int mat[4][4] = { {10, 20, 30, 40},
                    {15, 25, 35, 45},
                    {27, 29, 37, 48},
                    {32, 33, 39, 50}};
search(mat, 4, 31);
 
return 0;
} 

Saturday, October 13, 2018

Anagrams of a string

import java.util.*;
import java.lang.*;
import java.io.*;

class Anagram
{

public static String swap(String s, int i, int j){
char[] carr = s.toCharArray();
char tmp = carr[i];
carr[i] = carr[j];
carr[j] = tmp;
return new String(carr);
}

static int count = 0;

public static void anagram(String s, int start, int end){
if(start == end){
System.out.println(s);
count++;
return;
}

for(int i = start;i<=end; i++){
s = swap(s, start, i);
anagram(s, start+1, end);
s = swap(s, start, i);
}
}

public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String s = "ABCD";
anagram(s, 0, s.length() -1 );
System.out.println(count);
}
}

Monday, July 2, 2018

Trie Implementation with Map

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
    private static final Scanner scanner = new Scanner(System.in);

    private static class HashedTrie {
        private class Trie{
            int count;
            Map<Character, Trie> children;
            Boolean isLeafNode;

            public Trie() {
                count = 0;
                isLeafNode = false;
                children = new HashMap<Character, Trie>();
            }

            public Trie setChild(char ch){
                return children.put(ch, new Trie());
            }

            public Trie getChild(char ch){
                return children.get(ch);
            }

            public int getCount(){
                return count;
            }

            public void setCount(int count){
                this.count = count;
            }

            public Boolean isLeafNode(){
                return isLeafNode;
            }

            public void setLeafNode(Boolean isLeafNode){
                this.isLeafNode = isLeafNode;
            }
        }
     
        Trie trie;
     
        public HashedTrie(){
            trie = new Trie();
        }
     
        public Boolean InsertString(String s){
            Trie head = trie;
            head.setCount(head.getCount() + 1);
            for(char sChar: s.toCharArray()){
                if(head.getChild(sChar) == null){
                    head.setChild(sChar);
                }
                head = head.getChild(sChar);
                head.setCount(head.getCount() + 1);
            }
         
            head.setLeafNode(true);
            return true;
        }
     
        public int partialSearch(String s){
            Trie head = trie;
            for(char ch : s.toCharArray()){
                head = head.getChild(ch);
             
                if(head == null){
                    return 0;
                }
            }
         
            return head.getCount();
        }
    }
 
    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        List<String> list = new ArrayList();
        HashedTrie trie = new HashedTrie();
     
        for (int nItr = 0; nItr < n; nItr++) {
            String[] opContact = scanner.nextLine().split(" ");

            String op = opContact[0];

            String contact = opContact[1];
         
            if(op.contains("add")) {
                trie.InsertString(contact);
            }
         
            if(op.contains("find")){
                int count = trie.partialSearch(contact);
                System.out.println(count);
            }
        }

        scanner.close();
    }
}

Thursday, June 28, 2018

Rotating an array by n

    static int gcd(int a , int b){
        if(b == 0){
            return a;
        } else {
            return gcd(b, a%b);
        }
    }
   
    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        if(a.length <= 1) {
            return a;
        }
       
        int length = a.length;
       
        for(int i = 0 ; i < gcd(length, d) ; i++) {
            int temp = a[i];
            int j = i;
            while(true) {
                int k = j + d;
                if(k >= length) {
                    k = k - length;
                }
               
                if(k == i) {
                    break;
                }
                a[j] = a[k];
                j=k;
            }
            a[j] = temp;
        }
       
        return a;
    }