Tuesday 20 September 2016

Permutations (leetcode)

Problem: Permutations
Given a collection of distinct numbers, return all possible permutations.

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        
        int arrayLength = nums.size();
        vector< vector<int> > allPossiblePermutation;
        recursiveCall(0, arrayLength, allPossiblePermutation, nums);
        return allPossiblePermutation;
    }
    
    void recursiveCall(int left, int right, vector<vector<int>>& allPossiblePermutation, vector<int>& nums)
    {
        if(left==right-1)
        {
            allPossiblePermutation.push_back(nums);
            return;
        }
        for(int i=left;i<right;i++)
        {
            swap(nums[left], nums[i]);
            recursiveCall(left+1, right, allPossiblePermutation, nums);
            swap(nums[left], nums[i]);
        }
    }
};

Implement Trie / Prefix Tree (leetcode)

Problem: Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods
Note:
You may assume that all inputs are consist of lowercase letters a-z.

class TrieNode {
public:
    // Initialize your data structure here.
    TrieNode *child[26];
    int countPrefix;
    bool isEnd;
    TrieNode() {
        countPrefix = 0;
        isEnd =false;
        for(int i=0;i<26;i++)
            child[i]=NULL;
    }
};

class Trie {
public:
    Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    void insert(string word) {
        TrieNode *head = root;
        
        int sl=word.length();
        for(int i=0;i<sl;i++)
        {
            int index = word[i] - 'a';
            if(head->child[index]==NULL)
                head->child[index] = new TrieNode();
            head->child[index]->countPrefix++;
            head = head->child[index];
        }
        head->isEnd=true;
    }

    // Returns if the word is in the trie.
    bool search(string word) {
        TrieNode *head = getLastNode(word);
        if(head==NULL) return false;
        return head->isEnd;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    bool startsWith(string prefix) {
        TrieNode *head = getLastNode(prefix);
        if(head==NULL) return false;
        if(head->countPrefix) return true;
        return false;
    }
    
    TrieNode* getLastNode(string s)
    {
        TrieNode *head = root;
        
        int sl=s.length();
        for(int i=0;i<sl;i++)
        {
            int index = s[i]-'a';
            if(head->child[index]==NULL)
                return NULL;
            head = head->child[index];
        }
        return head;
    }

private:
    TrieNode* root;
};

// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

Monday 19 September 2016

Set Matrix Zeroes (leetcode)

Problem: Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0.

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        
        int row = matrix.size();
        int col = matrix[0].size();
        
        bool row0[row], col0[col];
        fill_n(row0, row, true);
        fill_n(col0, col, true);
        
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
                if(matrix[i][j]==0)
                    row0[i]=col0[j]=false;
        for(int i=0;i<row;i++)
            if(!row0[i])
                for(int j=0;j<col;j++)
                    matrix[i][j]=0;
        for(int i=0;i<col;i++)
            if(!col0[i])
                for(int j=0;j<row;j++)
                    matrix[j][i]=0;
        return;
    }
};