Thursday, 28 October 2021

Robot Return to Origin - LeetCode 657

 There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).

Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.

Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

 

Example 1:

Input: moves = "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

Example 2:

Input: moves = "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

Example 3:

Input: moves = "RRDD"
Output: false

Example 4:

Input: moves = "LDRRLRUULR"
Output: false

 

Solution:

class Solution {

    public boolean judgeCircle(String moves) {

        

        int x=0,y=0;

        

        for(char ch: moves.toCharArray()){

            

            switch(ch){

                case 'U': ++y; break;

                case 'D': --y; break;

                case 'L': --x; break;

                case 'R': ++x; break;

            }

            

        }

        

        if(x==0 && y==0)

            return true;

        else 

            return false;

        

    }

}


Symmetric Tree - LeetCode 101

 Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

 

Example 1:

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

Input: root = [1,2,2,null,3,null,3]
Output: false

 

Solution:

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode() {}

 *     TreeNode(int val) { this.val = val; }

 *     TreeNode(int val, TreeNode left, TreeNode right) {

 *         this.val = val;

 *         this.left = left;

 *         this.right = right;

 *     }

 * }

 */

class Solution {

    public boolean isSymmetric(TreeNode root) {

        return isMirror(root, root);

    }

    public boolean isMirror(TreeNode node1, TreeNode node2){

        

        if(node1 == null && node2 == null) return true;

        if(node1 == null || node2 == null) return false;

        

        return (node1.val==node2.val) && isMirror(node1.left,node2.right) && isMirror(node1.right,node2.left);

    }

}

Explanation:

We are taking that same root twice and traversing one to left half of the tree and other root to second half of the tree and comparing if the values are equal or not.


Monday, 18 October 2021

Invert Binary Tree - LeetCode 226

 Given the root of a binary tree, invert the tree, and return its root.

 

Example 1:

Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]

Example 2:

Input: root = [2,1,3]
Output: [2,3,1]

Example 3:

Input: root = []
Output: []

 

Solution:

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode() {}

 *     TreeNode(int val) { this.val = val; }

 *     TreeNode(int val, TreeNode left, TreeNode right) {

 *         this.val = val;

 *         this.left = left;

 *         this.right = right;

 *     }

 * }

 */

class Solution {

    public TreeNode invertTree(TreeNode root) {

        

        if(root==null) return root;

        

        TreeNode left=invertTree(root.left);

        TreeNode right=invertTree(root.right);

        

        root.left=right;

        root.right=left;

        

        return root;

    }

}

Saturday, 16 October 2021

Merge Two Binary Trees - LeetCode 617

 You are given two binary trees root1 and root2.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

Return the merged tree.

Note: The merging process must start from the root nodes of both trees.

 

Example 1:

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output: [3,4,5,5,4,null,7]

Example 2:

Input: root1 = [1], root2 = [1,2]
Output: [2,2]

 

Solution:

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode() {}

 *     TreeNode(int val) { this.val = val; }

 *     TreeNode(int val, TreeNode left, TreeNode right) {

 *         this.val = val;

 *         this.left = left;

 *         this.right = right;

 *     }

 * }

 */

class Solution {

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {

     

        if(root1==null)

            return root2;

        if(root2==null)

            return root1;

        

        root1.val=root1.val+root2.val;

        root1.left=mergeTrees(root1.left,root2.left);

        root1.right=mergeTrees(root1.right,root2.right);

     

        return root1;

    }

}

First Unique Character in a String - LeetCode 387

 Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

 

Example 1:

Input: s = "leetcode"
Output: 0

Example 2:

Input: s = "loveleetcode"
Output: 2

Example 3:

Input: s = "aabb"
Output: -1

 

Solution:

class Solution {

    public int firstUniqChar(String s) {

        

        Map<Character,Integer> map=new HashMap<>();

        int result=-1,count=0;

        

        for(int i=0;i<s.length();i++){

            char ch=s.charAt(i);

            if(map.containsKey(ch))

                map.put(ch,map.get(ch)+1);

            else

                map.put(ch,1);

        }

       

        for(int i=0;i<s.length();i++){

            char ch=s.charAt(i);

            if(map.get(ch)==1){

                return i;

            }

            

        }

        

        return -1;

    }

}

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...