Friday 14 January 2022

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(String[] args) {

    Scanner in=new Scanner(System.in);

    

    System.out.println("How many pw do you need");

    

    int total=in.nextInt();

    

    System.out.println("How long");

    int len=in.nextInt();

    

    // '0'-'9' --> 48 - 57

    // 'A' - 'Z' --> 65-90

    // 'a' - 'z' --> 97-122

    

    ArrayList<String> listOfPasswords=new ArrayList<>();

    

    // for looping total number of passwords

    for(int i=0;i<total;i++){

        // generate one random password

        

        String randomPassword="";

        for(int j=0;j<len;j++){

            //generate one random character

            randomPassword=randomPassword+randomCharacter();

            

        }

     

        listOfPasswords.add(randomPassword);

    }

  for(String i: listOfPasswords)  

System.out.println(i+"");

}

public static char randomCharacter(){

    

    // generate a random number represents all possible character 

    // in out psword

    

    // 10 digits+26 uppercase+ 26 lower case = 62 possible values

    

    int rand=(int)(Math.random()*62);

    

    if(rand<=9){

        

        rand=rand + 48;

        return (char)(rand);

    }else if(rand<=35){

        rand = rand + 55;

        return (char)(rand);

    }else{

        rand = rand + 61;

        return (char)(rand);

    }

  

}

}


Tuesday 16 November 2021

Best Time to Buy and Sell Stock II - LeetCode 112

 You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.

Find and return the maximum profit you can achieve.

 

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.

Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.

Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

 

Solution:

class Solution {

    public int maxProfit(int[] prices) {

        int profit=0;

        for(int i=1;i<prices.length;i++){

            if(prices[i]>prices[i-1]){

                profit=profit+(prices[i]-prices[i-1]);

            }

        }

        return profit;

    }

}

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;

    }

}

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...