Showing posts with label geeks for geeks. Show all posts
Showing posts with label geeks for geeks. 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);

    }

  

}

}


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;

    }

}

Monday, 4 October 2021

Maximum difference of zeros and ones in binary string - geeksforgeeks

 Given a binary string S consisting of 0s and 1s. The task is to find the maximum difference of the number of 0s and the number of 1s (number of 0s – number of 1s) in the substrings of a string.

Note: In the case of all 1s, the answer will be -1.

Example 1:

Input : S = "11000010001" 
Output : 6 
Explanatio: From index 2 to index 9, 
there are 7 0s and 1 1s, so number 
of 0s - number of 1s is 6. 

Example 2:

Input: S = "111111"
Output: -1
Explanation: S contains 1s only 

Your task:
You do not need to read any input or print anything. The task is to complete the function maxSubstring(), which takes a string as input and returns an integer.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

Constraints:
1 ≤ |S| ≤ 105
S contains 0s and 1s only


Solution(Only function):


class Solution {

    int maxSubstring(String S) {

        // code here

        

        int max_until=0;

        int ma=Integer.MIN_VALUE;

        

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

            

            int x=(S.charAt(i)=='0')?1:-1;

            

            max_until+=x;

            

            if(max_until>ma) ma=max_until;

            

            if(max_until<0) max_until=0;

        }

        return ma;

        

    }

}

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