Showing posts with label GeeksForGeeks. Show all posts
Showing posts with label GeeksForGeeks. Show all posts

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;

    }

}

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;

    }

}

Thursday, 14 October 2021

Trapping Rain Water - LeetCode 42

 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

 

Example 1:

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.

Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9

 

Solution 1(Optimal approach):

Space complexity: O(1), Time complexity: O(n)

class Solution {

    public int trap(int[] height) {

        

        if(height.length==1 || height.length==2)

            return 0;

        

        int n=height.length;

        int maxLeft=height[0],maxRight=height[n-1];

        int trappedWater=0;

        

        int leftptr=1,rightptr=n-2;                       

            

        while(leftptr<=rightptr){

          

          int min=0,diff=0;

            

          if(height[leftptr]>maxLeft)

              maxLeft=height[leftptr];

          if(height[rightptr]>maxRight)

              maxRight=height[rightptr];

            

          if(maxLeft<=maxRight)

          {

            min=Math.min(maxLeft,maxRight);  

            diff=min-height[leftptr];

              leftptr++;

          }else

            {

                min=Math.min(maxLeft,maxRight);

                diff=min-height[rightptr];

                rightptr--;

          }     

            

          trappedWater=trappedWater+diff;  

        }

        return trappedWater;

    }

}


Solution 2: (Space complexity: O(n), Time complexity: O(n))

class Solution {

    public int trap(int[] height) {

        

        if(height.length==1 || height.length==2)

            return 0;

        

        int n=height.length;

        int maxLeft[]=new int[n],maxRight[]=new int[n];

        int trappedWater=0;

        

        int leftMax=0,rightMax=0;                       

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

            

            if(height[i]>leftMax)

                leftMax=height[i];

            

            maxLeft[i]=leftMax;

            

        }

        for(int i=n-1;i>=0;i--){

            

            if(height[i]>rightMax)

                rightMax=height[i];

            

            maxRight[i]=rightMax;

            

        }

                               

        for(int i=1;i<n-1;i++){

            

            int min=Math.min(maxRight[i],maxLeft[i]);

            int diff=min-height[i];

            

            trappedWater=trappedWater+diff;

            

        }                       

                               

        return trappedWater;

    }

}

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