├── 1020_Number_Of_Enclaves.java ├── 1022_Sum_Of_Root_To_Leaf_Binary_Numbers.java ├── 1026_Maximum_Difference_Between_Node_And_Ancestor.java ├── 113_Path_Sum_II.java ├── 1161_Maximum_Level_Sum_of_Binary_Tree.java ├── 1299_Replace_Elements_with_Greatest_Element_on_Right_Side.java ├── 1315.java ├── 1315_Sum_Of_Nodes_With_Even_Valued_Grandparent.java ├── 1376_Time_Needed_to_Inform_All_Employees.java ├── 1377_Frog_Position_After_T_Seconds.java ├── 1436_Destination_City.java ├── 1603_Design_Parking_System.java ├── 1653_Minimum_Deletions_To_Make_String_Balanced.java ├── 1669_Merge_In_Between_Linked_Lists.java ├── 1768_Merge_Strings_Alternately.java ├── 1791_Find_Center_of_Star_Graph.java ├── 1817_Finding_the_Users_Active_Minutes.java ├── 1901_Find_Peak_Element_II.java ├── 1957_Delete_Characters_to_Make_Fancy_String.java ├── 1961_Check_If_String_Is_Prefix_Of_Array.java ├── 1970_Last_Day_Where_You_Can_Still_Cross.java ├── 1980_Find_Unique_Binary_String.java ├── 1985_Find_the_Kth_Largest_Integer_in_the_Array.java ├── 206_Reverse_Linked_List.java ├── 20_Valid_Parentheses.java ├── 2116_Check_If_A_Parentheses_String_Can_Be_Valid.java ├── 2290_Minimum_Obstacle_Removal_To_Reach_Corner.java ├── 229_Majority_Element_II.java ├── 257_Binary_Tree_Paths.java ├── 322_Coin_Change.java ├── 419_Battleships_In_A_Board.java ├── 429_N-ary_Tree_Level_Order_Traversal.java ├── 451_Sort_Characters_By_Frequency.java ├── 472_Concatenated_Words.java ├── 49_Group_Anagrams.java ├── 501_Find_Mode_in_Binary_Search_Tree.java ├── 513_Find_Bottom_Left_Tree_Value.java ├── 518_Coin_Change_2.java ├── 557_Reverse_Words_In_String_III.java ├── 61_Rotate_List.java ├── 695_Max_Area_Of_Island.java ├── 75_Sort_Colors.java ├── 796_Rotate_String.java ├── 797_All_Paths_From_Source_To_Target.java ├── 827_Making_A_Large_Island.java ├── 86_Partition_List.java ├── 916_Word_Subsets.java ├── 949_Largest_Time_For_Given_Digits.java ├── 988_Smallest_String_Starting_From_Leaf.java ├── 98_Validate_Binary_Search_Tree.java ├── 994_Rotting_Oranges.java ├── Leetcode1024.java ├── Leetcode1612.java ├── Leetcode_1604_Alert_Using_Same_Key_Card_Three_Or_More_Times_In_A_One_Hour_Period.java ├── Leetcode_165_Compare_Version_Numbers.java ├── Leetcode_2105.java ├── Leetcode_2125.java ├── Leetcode_2130.java ├── Leetcode_2131.java ├── Leetcode_2133_Check_If_Every_Row_And_Column_Contains_All_Numbers.java ├── Leetcode_2135.java ├── Leetcode_2135_Count_Words_Obtained_After_Adding_A_Letter.java ├── Leetcode_2150_Find_All_Lonely_Numbers_In_The_Array.java ├── Leetcode_802.java ├── binary_tree_top_bottom_view ├── BinaryTreeBottomView.java ├── BinaryTreeTopView.java └── btree.png ├── collections_java.docx ├── dynamic_programming ├── LCS.png ├── LongestCommonSubstring.java ├── MinimumMatrixCost.java └── MinimumMatrixCost.png ├── interview_questions.txt ├── lag_function_mysql.sql ├── lead_function_mysql.sql ├── unix_commands.docx └── unix_commands_for_interview /1020_Number_Of_Enclaves.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int ROWS, COLS; 4 | 5 | public int numEnclaves(int[][] grid) { 6 | ROWS = grid.length; 7 | COLS = grid[0].length; 8 | 9 | int count = 0; 10 | 11 | for (int i = 0; i < ROWS; i++) { 12 | for (int j = 0; j < COLS; j++) { 13 | // if we find 1 on the boundary of the matrix, we will do dfs 14 | if ((i == 0 || j == 0 || i == ROWS - 1 || j == COLS - 1) && grid[i][j] == 1) { 15 | dfs(grid, i, j); 16 | } 17 | } 18 | } 19 | 20 | for (int i = 0; i < ROWS; i++) { 21 | for (int j = 0; j < COLS; j++) { 22 | // if we still have 1 remaining inside of the matrix, it means that we cannot 23 | // walk off the boundary of the grid 24 | // from those cells, so we will coun them 25 | if (grid[i][j] == 1) { 26 | count++; 27 | } 28 | } 29 | } 30 | return count; 31 | } 32 | 33 | 34 | private void dfs(int[][] grid, int i, int j) { 35 | if (i < 0 || j < 0 || i >= ROWS || j >= COLS || grid[i][j] == 0) { 36 | return; 37 | } 38 | 39 | grid[i][j] = 0; 40 | 41 | dfs(grid, i - 1, j); 42 | dfs(grid, i + 1, j); 43 | dfs(grid, i, j - 1); 44 | dfs(grid, i, j + 1); 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /1022_Sum_Of_Root_To_Leaf_Binary_Numbers.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1022. Sum of Root To Leaf Binary Numbers 3 | /////////////////////////////////////////// 4 | 5 | 6 | /** 7 | * Definition for a binary tree node. 8 | * public class TreeNode { 9 | * int val; 10 | * TreeNode left; 11 | * TreeNode right; 12 | * TreeNode() {} 13 | * TreeNode(int val) { this.val = val; } 14 | * TreeNode(int val, TreeNode left, TreeNode right) { 15 | * this.val = val; 16 | * this.left = left; 17 | * this.right = right; 18 | * } 19 | * } 20 | */ 21 | class Solution { 22 | public int sumRootToLeaf(TreeNode root) { 23 | 24 | int sum = 0; 25 | List> list = new ArrayList>(); 26 | traverse(root, new ArrayList(), list); 27 | //System.out.println("list : "+list); 28 | 29 | 30 | for(List l : list) { 31 | StringBuilder sb = new StringBuilder(); 32 | for(int i=0; i< l.size(); i++) { 33 | sb.append(l.get(i)); 34 | } 35 | 36 | sum+= Integer.parseInt(sb.toString(), 2); 37 | } 38 | 39 | return sum; 40 | 41 | } 42 | 43 | private void traverse(TreeNode root, List list, List> output) { 44 | 45 | if(root == null) { 46 | return; 47 | } 48 | 49 | if(root.left == null && root.right == null) { 50 | list.add(String.valueOf(root.val)); 51 | 52 | List newlist=new ArrayList(list); 53 | 54 | output.add(newlist); 55 | 56 | list.remove(list.size()-1); 57 | 58 | return; 59 | } 60 | 61 | list.add(String.valueOf(root.val)); 62 | 63 | traverse(root.left, list, output); 64 | traverse(root.right, list, output); 65 | 66 | list.remove(list.size()-1); // backtracking 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /1026_Maximum_Difference_Between_Node_And_Ancestor.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1026. Maximum Difference Between Node and Ancestor 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | int maxdiff = Integer.MIN_VALUE; 23 | 24 | public int maxAncestorDiff(TreeNode root) { 25 | 26 | LinkedList list = new LinkedList(); 27 | findMaxDifference(root, list); 28 | return maxdiff; 29 | } 30 | 31 | 32 | public void findMaxDifference(TreeNode root, List list){ 33 | 34 | if(root == null) { 35 | return; 36 | } 37 | 38 | if(root.left == null && root.right ==null) { 39 | list.add(root.val); 40 | 41 | Collections.sort(list); 42 | maxdiff=Math.max(maxdiff,Math.abs(list.get(0)-list.get(list.size()-1))); 43 | return; 44 | } 45 | 46 | list.add(root.val); 47 | 48 | findMaxDifference(root.left, new ArrayList(list)); 49 | findMaxDifference(root.right, new ArrayList(list)); 50 | 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /113_Path_Sum_II.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //198_House_Robber 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public int rob(int[] nums) { 7 | 8 | if(nums.length==1) { 9 | return nums[0]; 10 | } 11 | 12 | int max = 0; 13 | int N = nums.length; 14 | 15 | int dp[] = new int[nums.length]; 16 | 17 | dp[0] = nums[0]; 18 | dp[1] = Math.max(nums[0], nums[1]); 19 | 20 | for(int i=2 ; i < N ; i++) { 21 | dp[i] = Math.max( dp[i-1] , dp[i-2] + nums[i]); 22 | } 23 | 24 | return dp[N-1]; 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /1161_Maximum_Level_Sum_of_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | 1161. Maximum Level Sum of a Binary Tree 3 | /************************************************************* 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | int maxlevelsum = 0; 22 | 23 | public int maxLevelSum(TreeNode root) { 24 | bfs(root); 25 | return this.maxlevelsum; 26 | } 27 | 28 | private void bfs(TreeNode root) { 29 | 30 | Queue queue = new java.util.LinkedList(); 31 | 32 | int level = 1; 33 | int sum = 0; 34 | int maxsum = Integer.MIN_VALUE; 35 | int maxlevelsum = 0; 36 | 37 | queue.add(root); 38 | 39 | 40 | while (!queue.isEmpty()) { 41 | 42 | int size = queue.size(); 43 | sum = 0; 44 | 45 | // iterate all nodes in that level and calculate the sum 46 | for (int i = 0; i < size; i++) { 47 | TreeNode n = queue.poll(); 48 | 49 | sum += n.val; 50 | 51 | if (n.left != null) 52 | queue.add(n.left); 53 | 54 | if (n.right != null) 55 | queue.add(n.right); 56 | } 57 | 58 | 59 | if(sum > maxsum) { 60 | maxsum = sum; 61 | this.maxlevelsum = level; 62 | } 63 | 64 | 65 | 66 | level++; 67 | } 68 | 69 | //System.out.println("maxlevelsum:" + maxlevelsum); 70 | 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /1299_Replace_Elements_with_Greatest_Element_on_Right_Side.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1299. Replace Elements with Greatest Element on Right Side 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public int[] replaceElements(int[] arr) { 7 | int greatest = -1; 8 | 9 | for(int i=arr.length-1;i>=0;i--) { 10 | int temp = arr[i]; 11 | 12 | arr[i]=greatest; 13 | 14 | if(temp>greatest) { 15 | greatest = temp; 16 | } 17 | } 18 | 19 | return arr; 20 | } 21 | } -------------------------------------------------------------------------------- /1315.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int sumEvenGrandparent(TreeNode root) { 18 | int output = 0; 19 | 20 | if(root == null) { 21 | return 0; 22 | } 23 | 24 | List list = new LinkedList(); 25 | traverse(root,list); 26 | 27 | List result = list.stream().filter(x-> x!=null).collect(Collectors.toList()); 28 | 29 | for(int i = 0 ; i < result.size() ; i++) { 30 | output+= result.get(i).val; 31 | } 32 | 33 | return output; 34 | } 35 | 36 | 37 | private void traverse(TreeNode root, List list) { 38 | 39 | if(root==null) { 40 | return; 41 | } 42 | 43 | 44 | if(root.val%2==0) { 45 | if(root.left!=null) { 46 | list.add(root.left.left); 47 | list.add(root.left.right); 48 | } 49 | 50 | if(root.right!=null) { 51 | list.add(root.right.left); 52 | list.add(root.right.right); 53 | } 54 | } 55 | 56 | traverse(root.left, list); 57 | traverse(root.right, list); 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /1315_Sum_Of_Nodes_With_Even_Valued_Grandparent.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1315. Sum of Nodes with Even-Valued Grandparent 3 | /////////////////////////////////////////// 4 | 5 | 6 | /** 7 | * Definition for a binary tree node. 8 | * public class TreeNode { 9 | * int val; 10 | * TreeNode left; 11 | * TreeNode right; 12 | * TreeNode() {} 13 | * TreeNode(int val) { this.val = val; } 14 | * TreeNode(int val, TreeNode left, TreeNode right) { 15 | * this.val = val; 16 | * this.left = left; 17 | * this.right = right; 18 | * } 19 | * } 20 | */ 21 | class Solution { 22 | public int sumEvenGrandparent(TreeNode root) { 23 | int output = 0; 24 | 25 | if(root == null) { 26 | return 0; 27 | } 28 | 29 | List list = new LinkedList(); 30 | traverse(root,list); 31 | 32 | List result = list.stream().filter(x-> x!=null).collect(Collectors.toList()); 33 | 34 | for(int i = 0 ; i < result.size() ; i++) { 35 | output+= result.get(i).val; 36 | } 37 | 38 | return output; 39 | } 40 | 41 | 42 | private void traverse(TreeNode root, List list) { 43 | 44 | if(root==null) { 45 | return; 46 | } 47 | 48 | 49 | if(root.val%2==0) { 50 | if(root.left!=null) { 51 | list.add(root.left.left); 52 | list.add(root.left.right); 53 | } 54 | 55 | if(root.right!=null) { 56 | list.add(root.right.left); 57 | list.add(root.right.right); 58 | } 59 | } 60 | 61 | traverse(root.left, list); 62 | traverse(root.right, list); 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /1376_Time_Needed_to_Inform_All_Employees.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1376. Time Needed to Inform All Employees 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | int time_needed = 0 ; 8 | 9 | public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) 10 | { 11 | Map> graph = new HashMap<>(); 12 | 13 | for(int i= 0 ; i list = new ArrayList<>(); 20 | list.add(i); 21 | graph.put(manager[i],list); 22 | } 23 | else 24 | { 25 | ArrayList list = graph.get(manager[i]); 26 | list.add(i); 27 | graph.put(manager[i],list); 28 | } 29 | } 30 | 31 | calculateTime(headID,graph,informTime,0); 32 | 33 | return time_needed; 34 | } 35 | 36 | // recursive dfs function 37 | private void calculateTime(int headID, Map>graph, int[] informTime, int t) 38 | { 39 | // base case for recursion 40 | if(informTime[headID]==0) { 41 | return; 42 | } 43 | 44 | t = t + informTime[headID]; 45 | 46 | ArrayList temp = graph.get(headID); 47 | 48 | int s = temp.size(); 49 | 50 | for(int i = 0 ; i time_needed) { 54 | time_needed = t; 55 | } 56 | } 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /1377_Frog_Position_After_T_Seconds.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1377. Frog Position After T Seconds 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | 8 | 9 | public double frogPosition(int n, int[][] edges, int t, int target) { 10 | 11 | List[] graph = (List[]) new ArrayList[n + 1]; 12 | 13 | for (int i = 1; i <= n; i++) { 14 | graph[i] = new ArrayList<>(); 15 | } 16 | 17 | for (int[] edge : edges) { 18 | graph[edge[0]].add(edge[1]); 19 | graph[edge[1]].add(edge[0]); 20 | } 21 | 22 | boolean[] visited = new boolean[n + 1]; 23 | 24 | Queue queue = new LinkedList<>(); 25 | 26 | queue.offer(new int[] { 1, 1 }); 27 | 28 | visited[1] = true; 29 | 30 | int step = 0; 31 | 32 | //BFS Starts 33 | while (!queue.isEmpty() && step <= t) { 34 | 35 | int size = queue.size(); 36 | 37 | for (int i = 0; i < size; i++) { 38 | 39 | int[] current = queue.poll(); 40 | 41 | int b = 0; 42 | 43 | for (int w : graph[current[0]]) { 44 | if (!visited[w]) 45 | b++; 46 | } 47 | 48 | //frog reached target and (b is zero or t seconds has elapsed) 49 | if (current[0] == target && (b == 0 || step == t)) { 50 | //we will calculate the probability and return it. 51 | return 1.0 / current[1]; 52 | 53 | } 54 | 55 | for (int w : graph[current[0]]) { 56 | 57 | if (!visited[w]) { 58 | visited[w] = true; 59 | 60 | int temp = current[1] * b; 61 | 62 | queue.offer(new int[] { w, temp }); 63 | } 64 | } 65 | } 66 | 67 | step++; 68 | } 69 | 70 | return 0.0; 71 | } 72 | 73 | 74 | } -------------------------------------------------------------------------------- /1436_Destination_City.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1436. Destination City 3 | /////////////////////////////////////////// 4 | 5 | 6 | class Solution { 7 | public String destCity(List> paths) { 8 | String result=""; 9 | 10 | Set from = new HashSet(); 11 | Set to = new HashSet(); 12 | 13 | for(List l : paths) { 14 | 15 | from.add(l.get(0)); 16 | to.add(l.get(1)); 17 | } 18 | 19 | Iterator it = to.iterator(); 20 | 21 | while(it.hasNext()){ 22 | String city=it.next(); 23 | if(!from.contains(city)) { 24 | result=city; 25 | break; 26 | } 27 | 28 | } 29 | 30 | return result; 31 | } 32 | } -------------------------------------------------------------------------------- /1603_Design_Parking_System.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1603. Design Parking System 3 | /////////////////////////////////////////// 4 | 5 | class ParkingSystem { 6 | 7 | int bigslots; 8 | int mediumslots; 9 | int smallslots; 10 | 11 | 12 | public ParkingSystem(int big, int medium, int small) { 13 | this.bigslots = big; 14 | this.mediumslots = medium; 15 | this.smallslots = small; 16 | } 17 | 18 | public boolean addCar(int carType) { 19 | if(carType == 1) { 20 | if(this.bigslots>0) { 21 | this.bigslots--; 22 | return true; 23 | } else { 24 | return false; 25 | } 26 | } else if(carType == 2) { 27 | if(this.mediumslots>0) { 28 | this.mediumslots--; 29 | return true; 30 | } else { 31 | return false; 32 | } 33 | } else if(carType == 3) { 34 | if(this.smallslots>0) { 35 | this.smallslots--; 36 | return true; 37 | } else { 38 | return false; 39 | } 40 | } 41 | 42 | return false; 43 | } 44 | } 45 | 46 | /** 47 | * Your ParkingSystem object will be instantiated and called as such: 48 | * ParkingSystem obj = new ParkingSystem(big, medium, small); 49 | * boolean param_1 = obj.addCar(carType); 50 | */ -------------------------------------------------------------------------------- /1653_Minimum_Deletions_To_Make_String_Balanced.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 1653 Minimum Deletions to Make String Balanced 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public static int minimumDeletions(String s) { 8 | 9 | int L = s.length(); 10 | 11 | int prefix = 0; 12 | 13 | int total_deletions = 0; 14 | 15 | for (int i = 0; i < L; i++) { 16 | 17 | if (s.charAt(i) == 'b') 18 | prefix++; 19 | else if (s.charAt(i) == 'a' && prefix > 0) { 20 | prefix--; 21 | total_deletions++; 22 | } 23 | 24 | } 25 | 26 | return total_deletions; 27 | } 28 | } -------------------------------------------------------------------------------- /1669_Merge_In_Between_Linked_Lists.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1669. Merge In Between Linked Lists 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * public class ListNode { 8 | * int val; 9 | * ListNode next; 10 | * ListNode() {} 11 | * ListNode(int val) { this.val = val; } 12 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 13 | * } 14 | */ 15 | class Solution { 16 | public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { 17 | 18 | 19 | ListNode h1 = list1; 20 | 21 | ListNode prev1 = null; 22 | ListNode curr1 = list1; 23 | 24 | 25 | int i = 0; 26 | 27 | while(i < a) { 28 | prev1 = curr1; 29 | curr1 = curr1.next; 30 | i++; 31 | } 32 | 33 | prev1.next = list2; 34 | 35 | i = 0; 36 | 37 | while(i <= b-a) { 38 | prev1 = curr1; 39 | curr1 = curr1.next; 40 | i++; 41 | } 42 | 43 | prev1.next = null; 44 | 45 | ListNode prev2 = null; 46 | ListNode curr2 = list1; 47 | 48 | while(curr2 != null) { 49 | prev2 = curr2; 50 | curr2 = curr2.next; 51 | } 52 | 53 | prev2.next = curr1; 54 | 55 | return h1; 56 | } 57 | } -------------------------------------------------------------------------------- /1768_Merge_Strings_Alternately.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1768. Merge Strings Alternately 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public String mergeAlternately(String word1, String word2) { 7 | int first=0; 8 | int second=0; 9 | 10 | StringBuilder sb = new StringBuilder(); 11 | 12 | while(first map=new HashMap(); 12 | 13 | // number of nodes 14 | int N =0; 15 | 16 | for(int i=0;i> iter = map.entrySet().iterator(); 40 | 41 | while(iter.hasNext()) { 42 | Entry e= iter.next(); 43 | int key=e.getKey(); 44 | int value=e.getValue(); 45 | 46 | if(value == N-1) { 47 | r=key; 48 | } 49 | 50 | 51 | } 52 | 53 | return r; 54 | } 55 | } -------------------------------------------------------------------------------- /1817_Finding_the_Users_Active_Minutes.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1817. Finding the Users Active Minutes 3 | /////////////////////////////////////////// 4 | 5 | import java.util.Map.Entry; 6 | 7 | class Solution { 8 | 9 | public int[] findingUsersActiveMinutes(int[][] logs, int k) { 10 | int[] result = new int[k]; 11 | 12 | Map> map = new HashMap(); 13 | 14 | for(int i = 0; i < logs.length ; i++) { 15 | int[] uam_log = logs[i]; 16 | int userid = uam_log[0]; 17 | int minute = uam_log[1]; 18 | 19 | if(map.get(userid)==null) { 20 | Set s = new HashSet(); 21 | s.add(minute); 22 | map.put(userid,s); 23 | } else { 24 | Set s = map.get(userid); 25 | s.add(minute); 26 | map.put(userid,s); 27 | } 28 | 29 | } 30 | 31 | Iterator>> iter = map.entrySet().iterator(); 32 | 33 | while(iter.hasNext()) { 34 | Entry> e= iter.next(); 35 | Set set=e.getValue(); 36 | 37 | // as we start indexing result from 1 38 | result[set.size()-1]++; 39 | } 40 | 41 | 42 | return result; 43 | } 44 | } -------------------------------------------------------------------------------- /1901_Find_Peak_Element_II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int[] findPeakGrid(int[][] mat) { 4 | int startRow = 0; 5 | int endRow = mat.length-1; 6 | 7 | 8 | while(endRow >= startRow) { 9 | int middleRow = startRow + (endRow - startRow)/2; 10 | 11 | //System.out.println("startRow : "+startRow+" endRow : "+endRow+" middleRow:"+middleRow); 12 | 13 | // will get max element position for that row 14 | int rowmax = maxRowElementPosition(mat[middleRow], mat[middleRow].length-1); 15 | 16 | // middle row is the first row 17 | if (middleRow == 0) { 18 | if (mat[middleRow][rowmax] > mat[middleRow + 1][rowmax]) { 19 | return new int[]{middleRow, rowmax}; 20 | } 21 | } 22 | 23 | //middle row is the last row 24 | if (middleRow == mat.length - 1) { 25 | if (mat[middleRow][rowmax] > mat[middleRow - 1][rowmax]) { 26 | return new int[]{middleRow, rowmax}; 27 | } 28 | } 29 | 30 | // checking max element of the row with it's upper and lower row 31 | if (mat[middleRow][rowmax] > mat[middleRow + 1][rowmax] && mat[middleRow][rowmax] > mat[middleRow - 1][rowmax]) { 32 | return new int[]{middleRow, rowmax}; 33 | } 34 | 35 | // if max is lesser than next rows same column element, will move startRow to the nextRow 36 | if (mat[middleRow][rowmax] < mat[middleRow + 1][rowmax]) { 37 | startRow = middleRow+1; 38 | } else { // otherwise move the endRow to current row -1 39 | endRow = middleRow -1; 40 | } 41 | } 42 | 43 | // we didn't find peak element in matrix 44 | return new int[]{-1, -1}; 45 | 46 | } 47 | 48 | private int maxRowElementPosition(int[] arr, int end) { 49 | int max = 0; 50 | 51 | 52 | for ( int i = 0; i <= end; i++){ 53 | if (arr[i] > arr[max]){ 54 | max = i; 55 | } 56 | } 57 | 58 | return max; 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /1957_Delete_Characters_to_Make_Fancy_String.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1957. Delete Characters to Make Fancy String 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public String makeFancyString(String s) { 7 | 8 | StringBuilder sb = new StringBuilder(); 9 | 10 | int i =0; 11 | char prev = '*'; 12 | 13 | int prev_count = 0; 14 | 15 | while(i < s.length()) { 16 | 17 | char current = s.charAt(i); 18 | 19 | if(current == prev) { 20 | if(prev_count < 1) { 21 | sb.append(current); 22 | } 23 | prev_count++; 24 | } else { 25 | sb.append(current); 26 | prev_count=0; 27 | prev=current; 28 | } 29 | 30 | i++; 31 | 32 | } 33 | 34 | return sb.toString(); 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /1961_Check_If_String_Is_Prefix_Of_Array.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1961. Check If String Is a Prefix of Array 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public boolean isPrefixString(String s, String[] words) { 7 | 8 | StringBuilder sb = new StringBuilder(); 9 | 10 | for(String str: words) { 11 | sb.append(str); 12 | if(sb.toString().equals(s)) { 13 | return true; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /1970_Last_Day_Where_You_Can_Still_Cross.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 1970 Last Day Where You Can Still Cross - Hard Problem 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | int[] dir = new int[] { 0, 1, 0, -1, 0 }; 8 | 9 | public int latestDayToCross(int row, int col, int[][] cells) { 10 | // Since in this we are having a range that 11 | // Last day could be in between 1 to cells.length 12 | // we will use binary search in addition with bfs 13 | // In bfs we will store the indexes 14 | // And since everything is 1 based subtract -1 from indexes 15 | 16 | int left = 1; 17 | 18 | int right = cells.length; 19 | 20 | int days = 0; 21 | 22 | //using binary search. 23 | //1) when we know that we can cross at some day, the last crossing day would be in the right half. 24 | //2) when we know that we can not cross at some day, the last crossing day would be in the left half. 25 | 26 | //we will use BFS to determine if we can do the crossing 27 | 28 | while (left <= right) { 29 | int mid = left + (right - left) / 2; 30 | 31 | System.out.println("left : "+left+ " right : "+right+ " mid : "+mid); 32 | 33 | //this is the 1st case 34 | if (topToBottomWalkPossible(cells, row, col, mid)) { 35 | days = mid; 36 | left = mid + 1; 37 | } else { //this is the second case 38 | right = mid - 1; 39 | } 40 | } 41 | return days; 42 | 43 | } 44 | 45 | public boolean topToBottomWalkPossible(int[][] cells, int row, int col, int dayAt) { 46 | int[][] grid = new int[row][col]; 47 | 48 | //we will first create the grid how it looks like on that day. 49 | for (int i = 0; i < dayAt; i++) { 50 | grid[cells[i][0] - 1][cells[i][1] - 1] = 1; 51 | } 52 | 53 | Queue queue = new LinkedList<>(); 54 | 55 | for (int c = 0; c < col; c++) { 56 | //row should be always zero and the cell value should be zero (that's when we can start from that cell to cross). 57 | //we will add those cells into the queue as starting point for the BFS. 58 | 59 | 60 | //once we add that cell into the queue, we will change it's value to 1 (to avoid re - processing) 61 | if (grid[0][c] == 0) { 62 | queue.offer(new int[] { 0, c }); 63 | grid[0][c] = 1; 64 | } 65 | } 66 | 67 | while (!queue.isEmpty()) { 68 | 69 | int current[] = queue.poll(); 70 | 71 | int new_row = current[0]; 72 | int new_col = current[1]; 73 | 74 | //checking do we reach the last row 75 | if (new_row == row - 1) 76 | return true; 77 | 78 | //doing the BFS in 4 directions 79 | 80 | for (int i = 0; i < 4; i++) { 81 | int x = new_row + dir[i], y = new_col + dir[i + 1]; 82 | 83 | if (x < 0 || x == row || y < 0 || y == col || grid[x][y] == 1) 84 | continue; 85 | 86 | grid[x][y] = 1; 87 | 88 | queue.offer(new int[] { x, y }); 89 | } 90 | 91 | } 92 | return false; 93 | } 94 | } -------------------------------------------------------------------------------- /1980_Find_Unique_Binary_String.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //1980. Find Unique Binary String 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public String findDifferentBinaryString(String[] nums) { 8 | String result=""; 9 | 10 | Set givenNumbersSet = new HashSet(); 11 | 12 | for(int i =0; i allBinaryStrSet = new HashSet(); 17 | 18 | generateBinaryStrings("", "", nums[0].length(), allBinaryStrSet, givenNumbersSet); 19 | 20 | return allBinaryStrSet.stream().findFirst().get(); 21 | } 22 | 23 | 24 | // generate binary strings of length "l". if it finds a string of length "l" which is not 25 | // in given input strings, it add it to result set and returns. 26 | 27 | private static void generateBinaryStrings(String s, String t, int l, Set set, Set givenNumbersSet) { 28 | 29 | if(s.length()>l) { 30 | return; 31 | } 32 | 33 | if(s.length()==l) { 34 | if(!givenNumbersSet.contains(s)) { 35 | set.add(s); 36 | return; 37 | } 38 | } 39 | 40 | s= s+t; 41 | 42 | generateBinaryStrings(s, "1", l, set, givenNumbersSet); 43 | generateBinaryStrings(s, "0", l, set, givenNumbersSet); 44 | } 45 | 46 | ======= 47 | /////////////////////////////////////////// 48 | //1980. Find Unique Binary String 49 | /////////////////////////////////////////// 50 | 51 | class Solution { 52 | public String findDifferentBinaryString(String[] nums) { 53 | String result=""; 54 | 55 | Set givenNumbersSet = new HashSet(); 56 | 57 | for(int i =0; i allBinaryStrSet = new HashSet(); 62 | 63 | generateBinaryStrings("", "", nums[0].length(), allBinaryStrSet, givenNumbersSet); 64 | 65 | return allBinaryStrSet.stream().findFirst().get(); 66 | } 67 | 68 | 69 | // generate binary strings of length "l". if it finds a string of length "l" which is not 70 | // in given input strings, it add it to result set and returns. 71 | 72 | private static void generateBinaryStrings(String s, String t, int l, Set set, Set givenNumbersSet) { 73 | 74 | if(s.length()>l) { 75 | return; 76 | } 77 | 78 | if(s.length()==l) { 79 | if(!givenNumbersSet.contains(s)) { 80 | set.add(s); 81 | return; 82 | } 83 | } 84 | 85 | s= s+t; 86 | 87 | generateBinaryStrings(s, "1", l, set, givenNumbersSet); 88 | generateBinaryStrings(s, "0", l, set, givenNumbersSet); 89 | } 90 | 91 | >>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 92 | } -------------------------------------------------------------------------------- /1985_Find_the_Kth_Largest_Integer_in_the_Array.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1985. Find the Kth Largest Integer in the Array 3 | /////////////////////////////////////////// 4 | 5 | import java.math.BigInteger; 6 | 7 | class Solution { 8 | 9 | 10 | public String kthLargestNumber(String[] nums, int k) { 11 | 12 | int N = nums.length; 13 | 14 | PriorityQueue pq = new PriorityQueue(); 15 | 16 | 17 | for(int i=0; i < N ; i++) { 18 | pq.offer(new BigInteger(nums[i])); 19 | } 20 | 21 | BigInteger number=null; 22 | int limit=(N-k+1); 23 | 24 | int i=0; 25 | 26 | while(i< limit) { 27 | number = pq.poll(); 28 | i++; 29 | } 30 | 31 | return number.toString(); 32 | 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /206_Reverse_Linked_List.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //206. Reverse Linked List 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * public class ListNode { 8 | * int val; 9 | * ListNode next; 10 | * ListNode() {} 11 | * ListNode(int val) { this.val = val; } 12 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 13 | * } 14 | */ 15 | class Solution { 16 | public ListNode reverseList(ListNode head) { 17 | ListNode curr = head; 18 | 19 | ListNode prev = null; 20 | 21 | ListNode next = null; 22 | 23 | while (curr != null) { 24 | next = curr.next; 25 | curr.next = prev; 26 | prev = curr; 27 | curr = next; 28 | } 29 | head = prev; 30 | 31 | return head; 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /20_Valid_Parentheses.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //20. Valid Parentheses 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public boolean isValid(String s) { 8 | Stack stack = new Stack(); 9 | 10 | for(int i =0; i stack = new Stack(); 60 | 61 | for(int i =0; i>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 104 | } -------------------------------------------------------------------------------- /2116_Check_If_A_Parentheses_String_Can_Be_Valid.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 2116 Check if a Parentheses String Can Be Valid 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public boolean canBeValid(String s, String locked) { 8 | 9 | int close_paranthesis = 0; 10 | int open_paranthesis = 0; 11 | int flip_allowed = 0; 12 | 13 | if (s.length() % 2 != 0) { 14 | return false; 15 | } 16 | 17 | for (int i = 0; i < s.length(); ++i) { 18 | 19 | if (locked.charAt(i) == '0') { 20 | flip_allowed++; 21 | } else { 22 | if (s.charAt(i) == '(') { 23 | open_paranthesis++; 24 | } else { 25 | close_paranthesis++; 26 | } 27 | } 28 | 29 | if (flip_allowed + open_paranthesis < close_paranthesis) { 30 | return false; 31 | } 32 | } 33 | 34 | close_paranthesis = 0; 35 | 36 | open_paranthesis = 0; 37 | 38 | flip_allowed = 0; 39 | 40 | for (int i = s.length() - 1; i >= 0; --i) { 41 | 42 | if (locked.charAt(i) == '0') { 43 | flip_allowed++; 44 | } else { 45 | 46 | if (s.charAt(i) == '(') { 47 | 48 | open_paranthesis++; 49 | } else { 50 | close_paranthesis++; 51 | } 52 | 53 | } 54 | 55 | if (flip_allowed + close_paranthesis < open_paranthesis) { 56 | return false; 57 | } 58 | } 59 | 60 | return true; 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /2290_Minimum_Obstacle_Removal_To_Reach_Corner.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int[][] dirs = new int[][] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; 4 | 5 | static class State { 6 | int row; 7 | int col; 8 | int removed; 9 | 10 | State(int r, int c, int removed) { 11 | this.row = r; 12 | this.col = c; 13 | this.removed = removed; 14 | } 15 | } 16 | 17 | public int minimumObstacles(int[][] grid) { 18 | 19 | int R = grid.length; 20 | 21 | int C = grid[0].length; 22 | 23 | //PriorityQueue keeps minimum removal states on the front 24 | Queue pq = new PriorityQueue<>((a, b) -> a.removed - b.removed); 25 | 26 | pq.add(new State(0, 0, 0)); 27 | 28 | boolean[][] visited = new boolean[R][C]; 29 | 30 | visited[0][0] = true; 31 | 32 | //BFS Starts 33 | while (!pq.isEmpty()) { 34 | 35 | State state = pq.poll(); 36 | 37 | //did we reach the destination 38 | if (state.row == R - 1 && state.col == C - 1) 39 | return state.removed; 40 | 41 | for (int[] d : dirs) { 42 | 43 | int new_row = state.row + d[0]; 44 | 45 | int new_col = state.col + d[1]; 46 | 47 | //checking if we are inside the grid and the new cell is not visited already 48 | if (new_row < 0 || new_col < 0 || new_row == R || new_col == C || visited[new_row][new_col]) 49 | continue; 50 | 51 | visited[new_row][new_col] = true; 52 | 53 | State next = new State(new_row, new_col, state.removed); 54 | 55 | if (grid[new_row][new_col] == 1) 56 | next.removed++; 57 | 58 | pq.add(next); 59 | } 60 | } 61 | 62 | return -1; 63 | } 64 | 65 | 66 | } -------------------------------------------------------------------------------- /229_Majority_Element_II.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //229. Majority Element II 3 | /////////////////////////////////////////// 4 | 5 | import java.util.Map.Entry; 6 | 7 | class Solution { 8 | 9 | public List majorityElement(int[] nums) { 10 | List result = new ArrayList(); 11 | 12 | Map map = new HashMap(); 13 | 14 | double threshold = nums.length / 3; 15 | 16 | for(int i = 0; i < nums.length; i++) { 17 | if(map.get(nums[i])==null) { 18 | map.put(nums[i],1d); 19 | } else { 20 | map.put(nums[i],map.get(nums[i]) + 1); 21 | } 22 | } 23 | 24 | 25 | Iterator> iter = map.entrySet().iterator(); 26 | 27 | while(iter.hasNext()) { 28 | Entry e = iter.next(); 29 | 30 | if(e.getValue() > threshold) { 31 | result.add(e.getKey()); 32 | } 33 | } 34 | 35 | return result; 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /257_Binary_Tree_Paths.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //257. Binary Tree Paths 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | public List binaryTreePaths(TreeNode root) { 22 | 23 | List result = new ArrayList(); 24 | 25 | List> list = new ArrayList>(); 26 | 27 | traverse(root, new ArrayList(), list); 28 | 29 | 30 | for(List l : list) { 31 | StringBuilder sb = new StringBuilder(); 32 | for(int i=0; i< l.size(); i++) { 33 | sb.append(l.get(i)+"->"); 34 | } 35 | 36 | result.add(sb.toString().substring(0,sb.toString().length()-2)); 37 | } 38 | 39 | return result; 40 | } 41 | 42 | 43 | private void traverse(TreeNode root, List list, List> output) { 44 | 45 | if(root == null) { 46 | return; 47 | } 48 | 49 | //once we reach the leaf node 50 | if(root.left == null && root.right == null) { 51 | list.add(String.valueOf(root.val)); 52 | 53 | List newlist=new ArrayList(list); 54 | 55 | output.add(newlist); 56 | 57 | list.remove(list.size()-1); // before we return, backtrack for last added element 58 | 59 | return; 60 | } 61 | 62 | list.add(String.valueOf(root.val)); 63 | 64 | traverse(root.left, list, output); 65 | traverse(root.right, list, output); 66 | 67 | list.remove(list.size()-1); // backtracking 68 | } 69 | } -------------------------------------------------------------------------------- /322_Coin_Change.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //322. Coin Change 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public int coinChange(int[] coins, int amount) { 7 | int[][] dp = new int[coins.length+1][amount+1]; 8 | 9 | boolean possible=true; 10 | if(amount==0) { 11 | return 0; 12 | } 13 | else if(coins.length==1 && (coins[0]>amount || amount%coins[0]!=0 )) { 14 | possible=false; 15 | 16 | } 17 | 18 | 19 | if(!possible) { 20 | return -1; 21 | } 22 | 23 | for(int i=1;i<=amount;i++) { 24 | dp[0][i]=i; 25 | } 26 | for(int i=0;i<=coins.length;i++) { 27 | dp[i][0]=0; 28 | } 29 | 30 | //populating remaining dp array 31 | for(int i=1;i<=coins.length;i++) { 32 | for(int j=1;j<=amount;j++) { 33 | 34 | if (j < coins[i-1]) { 35 | dp[i][j] = dp [i-1][j]; 36 | 37 | } else { 38 | dp[i][j] = Math.min(dp [i-1][j] , dp[i][j-coins[i-1]] +1 ) ; 39 | } 40 | 41 | } 42 | 43 | 44 | 45 | } 46 | 47 | 48 | return dp[coins.length][amount]; 49 | } 50 | } -------------------------------------------------------------------------------- /419_Battleships_In_A_Board.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //419. Battleships in a Board 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public int countBattleships(char[][] board) { 8 | int battleships=0; 9 | 10 | for (int i = 0; i < board.length; i++) { 11 | for (int j = 0; j < board[0].length; j++) { 12 | if(board[i][j]=='X') { 13 | battleships++; 14 | dfs(board, i, j); 15 | } 16 | 17 | } 18 | } 19 | 20 | return battleships; 21 | } 22 | 23 | 24 | public void dfs(char[][] board, int i, int j) { 25 | 26 | if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != 'X') { 27 | return; 28 | } 29 | 30 | board[i][j]='*'; 31 | 32 | //will make sure that we are changing whole island to some other character recursively so that it won't count again 33 | dfs(board, i-1, j); 34 | dfs(board, i, j+1); 35 | dfs(board, i+1, j); 36 | dfs(board, i, j-1); 37 | 38 | } 39 | 40 | ======= 41 | /////////////////////////////////////////// 42 | //419. Battleships in a Board 43 | /////////////////////////////////////////// 44 | 45 | class Solution { 46 | public int countBattleships(char[][] board) { 47 | int battleships=0; 48 | 49 | for (int i = 0; i < board.length; i++) { 50 | for (int j = 0; j < board[0].length; j++) { 51 | if(board[i][j]=='X') { 52 | battleships++; 53 | dfs(board, i, j); 54 | } 55 | 56 | } 57 | } 58 | 59 | return battleships; 60 | } 61 | 62 | 63 | public void dfs(char[][] board, int i, int j) { 64 | 65 | if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != 'X') { 66 | return; 67 | } 68 | 69 | board[i][j]='*'; 70 | 71 | //will make sure that we are changing whole island to some other character recursively so that it won't count again 72 | dfs(board, i-1, j); 73 | dfs(board, i, j+1); 74 | dfs(board, i+1, j); 75 | dfs(board, i, j-1); 76 | 77 | } 78 | 79 | >>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 80 | } -------------------------------------------------------------------------------- /429_N-ary_Tree_Level_Order_Traversal.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //429. N-ary Tree Level Order Traversal 3 | /////////////////////////////////////////// 4 | 5 | /* 6 | // Definition for a Node. 7 | class Node { 8 | public int val; 9 | public List children; 10 | 11 | public Node() {} 12 | 13 | public Node(int _val) { 14 | val = _val; 15 | } 16 | 17 | public Node(int _val, List _children) { 18 | val = _val; 19 | children = _children; 20 | } 21 | }; 22 | */ 23 | 24 | class Solution { 25 | public List> levelOrder(Node root) { 26 | 27 | List> result = new ArrayList(); 28 | 29 | if(root == null) { 30 | return result; 31 | } 32 | 33 | 34 | 35 | Queue queue = new LinkedList(); 36 | 37 | queue.offer(root); 38 | 39 | List list=new LinkedList(); 40 | list.add(root.val); 41 | result.add(list); 42 | 43 | 44 | // iterate through the queue until it becomes empty 45 | while(!queue.isEmpty()) { 46 | 47 | list = new LinkedList(); 48 | 49 | int size = queue.size(); 50 | 51 | // iterate through all the nodes for that level 52 | for(int i = 0; i < size ; i++) { 53 | Node node = queue.poll(); 54 | List children = node.children; 55 | 56 | for(int j = 0; j < children.size() ; j++) { 57 | Node temp = children.get(j); 58 | list.add(temp.val); 59 | queue.offer(temp); 60 | } 61 | 62 | } 63 | 64 | if(list.size() > 0) 65 | result.add(list); 66 | } 67 | 68 | return result; 69 | 70 | } 71 | 72 | 73 | 74 | 75 | } -------------------------------------------------------------------------------- /451_Sort_Characters_By_Frequency.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //451. Sort Characters By Frequency 3 | /////////////////////////////////////////// 4 | 5 | import java.util.Map.Entry; 6 | 7 | class Solution { 8 | public String frequencySort(String s) { 9 | 10 | Map map = new HashMap(); 11 | 12 | char[] chararr = s.toCharArray(); 13 | for (int i = 0; i < chararr.length; i++) { 14 | char ch = chararr[i]; 15 | 16 | if (map.get(ch) == null) { 17 | map.put(ch, 1); 18 | } else { 19 | map.put(ch, map.get(ch) + 1); 20 | } 21 | } 22 | 23 | 24 | Map> freqmap = new TreeMap(Collections.reverseOrder()); 25 | 26 | for (Entry entry : map.entrySet()) { 27 | 28 | Integer val = entry.getValue(); 29 | 30 | if(freqmap.get(val)==null) { 31 | List list= new ArrayList(); 32 | list.add(entry.getKey()); 33 | freqmap.put(entry.getValue(), list); 34 | } else { 35 | List list= freqmap.get(val); 36 | list.add(entry.getKey()); 37 | freqmap.put(entry.getValue(), list); 38 | } 39 | } 40 | 41 | //System.out.println(freqmap); 42 | 43 | StringBuilder sb = new StringBuilder(); 44 | 45 | for (Entry> entry : freqmap.entrySet()) { 46 | 47 | int key = entry.getKey(); 48 | List value = entry.getValue(); 49 | 50 | 51 | for (Character c : value) { 52 | for (int k = 0; k < key; k++) { 53 | sb.append(c); 54 | } 55 | } 56 | } 57 | 58 | return sb.toString(); 59 | } 60 | } -------------------------------------------------------------------------------- /472_Concatenated_Words.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 472 Concatenated Words - Hard Problem 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | static class TrieNode { 7 | boolean isWord = false; 8 | TrieNode[] children; 9 | 10 | TrieNode() { 11 | children = new TrieNode[26]; 12 | } 13 | } 14 | 15 | static class Trie { 16 | TrieNode root; 17 | 18 | Trie() { 19 | root = new TrieNode(); 20 | } 21 | 22 | public void insert(String word) { 23 | TrieNode node = root; 24 | for (char c : word.toCharArray()) { 25 | if (node.children[c - 'a'] == null) { 26 | node.children[c - 'a'] = new TrieNode(); 27 | } 28 | node = node.children[c - 'a']; 29 | } 30 | node.isWord = true; 31 | } 32 | 33 | public boolean search(String word) { 34 | TrieNode node = root; 35 | for (char c : word.toCharArray()) { 36 | if (node.children[c - 'a'] == null) { 37 | return false; 38 | } 39 | node = node.children[c - 'a']; 40 | } 41 | return node.isWord; 42 | } 43 | 44 | } 45 | 46 | public List findAllConcatenatedWordsInADict(String[] words) { 47 | Trie trie = new Trie(); 48 | 49 | //first we will insert all the words in a Trie 50 | for (String s : words) { 51 | trie.insert(s); 52 | } 53 | 54 | 55 | List result = new ArrayList<>(); 56 | 57 | //we will check for every word, if it can be splitted into multiple words such that each of the splitted word 58 | //exits in Trie. if it's the case, we found a concatenated word. 59 | for (String s : words) { 60 | if (util(trie, s, false)) 61 | result.add(s); 62 | } 63 | return result; 64 | } 65 | 66 | //we will use "cutWordPossible" to check if the given word can be splitted into more than one words. 67 | //Because if we can split the given word into multiple words, then only it's a concatenated word. 68 | private boolean util(Trie trie, String s, boolean cutWordPossible) { 69 | 70 | if (s.equals("") && cutWordPossible) 71 | return true; 72 | 73 | if (s.equals("")) 74 | return false; 75 | 76 | for (int i = 0; i < s.length(); i++) { 77 | 78 | String prefix = s.substring(0, i + 1); 79 | 80 | System.out.println("prefix:"+prefix); 81 | 82 | if (i == s.length() - 1 && !cutWordPossible) 83 | return false; 84 | 85 | if (trie.search(prefix)) { 86 | // cut = true; 87 | if (util(trie, s.substring(i + 1), true)) 88 | return true; 89 | } 90 | } 91 | 92 | return false; 93 | } 94 | } -------------------------------------------------------------------------------- /49_Group_Anagrams.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //49. Group Anagrams 3 | /////////////////////////////////////////// 4 | 5 | import java.util.Map.Entry; 6 | 7 | class Solution { 8 | public List> groupAnagrams(String[] strs) { 9 | List> result = new ArrayList(); 10 | 11 | Map> map = new HashMap(); 12 | 13 | for(int i = 0; i < strs.length; i++) { 14 | String s= strs[i]; 15 | char[] arr = s.toCharArray(); 16 | Arrays.sort(arr); 17 | String sorted = new String(arr); 18 | 19 | if(map.get(sorted)==null) { 20 | List list=new ArrayList(); 21 | list.add(s); 22 | map.put(sorted,list); 23 | } else { 24 | List list=map.get(sorted); 25 | list.add(s); 26 | map.put(sorted,list); 27 | } 28 | 29 | } 30 | 31 | 32 | Iterator>> iter = map.entrySet().iterator(); 33 | 34 | while(iter.hasNext()) { 35 | Entry> e = iter.next(); 36 | result.add(e.getValue()); 37 | } 38 | 39 | 40 | return result; 41 | } 42 | } -------------------------------------------------------------------------------- /501_Find_Mode_in_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //501. Find Mode in Binary Search Tree 3 | /////////////////////////////////////////// 4 | 5 | import java.util.Map.Entry; 6 | 7 | class Solution { 8 | 9 | class Element { 10 | int val; 11 | int freq; 12 | 13 | public Element(int v, int f) { 14 | this.val=v; 15 | this.freq=f; 16 | } 17 | } 18 | 19 | PriorityQueue pq =new PriorityQueue( 20 | (a,b) -> b.freq - a.freq 21 | ); 22 | 23 | Map map =new HashMap(); 24 | 25 | public int[] findMode(TreeNode root) { 26 | 27 | traverse(root); 28 | 29 | 30 | Iterator> iter = map.entrySet().iterator(); 31 | 32 | while(iter.hasNext()) { 33 | Entry e=iter.next(); 34 | pq.offer(new Element(e.getKey(),e.getValue())); 35 | } 36 | 37 | List list=new ArrayList(); 38 | 39 | Element el= pq.poll(); 40 | int v=el.val; 41 | int f=el.freq; 42 | list.add(v); 43 | 44 | while(!pq.isEmpty()) { 45 | Element curr = pq.poll(); 46 | int current=curr.freq; 47 | 48 | if(current==f) { 49 | list.add(curr.val); 50 | } else { 51 | break; 52 | } 53 | } 54 | 55 | int[] result = new int[list.size()]; 56 | for (int i=0; i < result.length; i++) 57 | { 58 | result[i] = list.get(i); 59 | } 60 | 61 | return result; 62 | } 63 | 64 | public void traverse(TreeNode root) { 65 | if(root==null) { 66 | return; 67 | } 68 | 69 | int v = root.val; 70 | 71 | if(map.get(v)!=null) { 72 | map.put(v, map.get(v)+1); 73 | } else { 74 | map.put(v, 1); 75 | } 76 | 77 | traverse(root.left); 78 | traverse(root.right); 79 | 80 | 81 | } 82 | } -------------------------------------------------------------------------------- /513_Find_Bottom_Left_Tree_Value.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //513. Find Bottom Left Tree Value 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | public int findBottomLeftValue(TreeNode root) { 22 | 23 | 24 | int result = 0; 25 | 26 | Queue q = new LinkedList(); 27 | 28 | q.offer(root); 29 | 30 | while(!q.isEmpty()) { 31 | 32 | int size = q.size(); 33 | 34 | boolean flag= true; 35 | 36 | for(int i = 0; i < size; i++) { 37 | 38 | TreeNode node = q.poll(); 39 | if(flag) { 40 | result=node.val; 41 | flag=false; 42 | } 43 | 44 | if(node.left!=null) 45 | q.offer(node.left); 46 | 47 | if(node.right!=null) 48 | q.offer(node.right); 49 | 50 | } 51 | 52 | } 53 | 54 | return result; 55 | } 56 | } -------------------------------------------------------------------------------- /518_Coin_Change_2.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //518. Coin Change 2 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public int change(int amount, int[] coins) { 7 | 8 | int[][] dp = new int[coins.length + 1][amount + 1]; 9 | 10 | for(int i = 0; i<= amount; i++) { 11 | dp[0][i] = 0; 12 | } 13 | 14 | for(int i = 1; i <= coins.length; i++) { 15 | dp[i][0] = 1; 16 | } 17 | 18 | //populating remaining dp array 19 | for(int i = 1; i <= coins.length; i++) { 20 | for(int j = 1; j <= amount; j++) { 21 | 22 | if (coins[i-1] > j) { 23 | dp[i][j] = dp [i-1][j]; 24 | } else { 25 | dp[i][j] = dp [i-1][j] + dp[i][j-coins[i-1]]; 26 | } 27 | 28 | } 29 | } 30 | 31 | return dp[coins.length][amount]; 32 | } 33 | } -------------------------------------------------------------------------------- /557_Reverse_Words_In_String_III.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //557. Reverse Words in a String III 3 | /////////////////////////////////////////// 4 | 5 | import java.util.StringTokenizer; 6 | 7 | class Solution { 8 | public String reverseWords(String s) { 9 | StringTokenizer st = new StringTokenizer(s, " "); 10 | StringBuilder sb = new StringBuilder(); 11 | 12 | while (st.hasMoreTokens()) { 13 | String str = st.nextToken(); 14 | sb.append(new StringBuilder(str).reverse().toString()+" "); 15 | } 16 | 17 | return sb.toString().substring(0,sb.toString().length()-1); 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /61_Rotate_List.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //61. Rotate List 4 | /////////////////////////////////////////// 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * public class ListNode { 9 | * int val; 10 | * ListNode next; 11 | * ListNode() {} 12 | * ListNode(int val) { this.val = val; } 13 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 14 | * } 15 | */ 16 | class Solution { 17 | public ListNode rotateRight(ListNode head, int k) { 18 | 19 | if(head==null || head.next==null) { 20 | return head; 21 | } 22 | 23 | ListNode curr = head; 24 | ListNode first = head; 25 | ListNode second = head; 26 | 27 | int i = 0; 28 | 29 | while(i < k) { 30 | // when k is greater than length of the list 31 | // we need to connect second back to the head 32 | if(second.next==null) { 33 | second=head; 34 | } else { 35 | second = second.next; 36 | } 37 | i++; 38 | } 39 | 40 | while(second.next!=null) { 41 | first = first.next; 42 | second = second.next; 43 | } 44 | 45 | second.next=head; 46 | head=first.next; 47 | first.next=null; 48 | 49 | return head; 50 | 51 | } 52 | ======= 53 | /////////////////////////////////////////// 54 | //61. Rotate List 55 | /////////////////////////////////////////// 56 | 57 | /** 58 | * Definition for singly-linked list. 59 | * public class ListNode { 60 | * int val; 61 | * ListNode next; 62 | * ListNode() {} 63 | * ListNode(int val) { this.val = val; } 64 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 65 | * } 66 | */ 67 | class Solution { 68 | public ListNode rotateRight(ListNode head, int k) { 69 | 70 | if(head==null || head.next==null) { 71 | return head; 72 | } 73 | 74 | ListNode curr = head; 75 | ListNode first = head; 76 | ListNode second = head; 77 | 78 | int i = 0; 79 | 80 | while(i < k) { 81 | // when k is greater than length of the list 82 | // we need to connect second back to the head 83 | if(second.next==null) { 84 | second=head; 85 | } else { 86 | second = second.next; 87 | } 88 | i++; 89 | } 90 | 91 | while(second.next!=null) { 92 | first = first.next; 93 | second = second.next; 94 | } 95 | 96 | second.next=head; 97 | head=first.next; 98 | first.next=null; 99 | 100 | return head; 101 | 102 | } 103 | >>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 104 | } -------------------------------------------------------------------------------- /695_Max_Area_Of_Island.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //695. Max Area of Island 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public int maxAreaOfIsland(int[][] grid) { 8 | int maxArea= Integer.MIN_VALUE; 9 | 10 | for (int i = 0; i < grid.length; i++) { 11 | for (int j = 0; j < grid[0].length; j++) { 12 | if(grid[i][j]==1) { 13 | 14 | int area = dfs(grid, i, j); 15 | maxArea = Math.max(maxArea, area); 16 | } 17 | 18 | } 19 | } 20 | 21 | return maxArea==Integer.MIN_VALUE? 0 : maxArea; 22 | } 23 | 24 | 25 | 26 | public int dfs(int[][] grid, int i, int j) { 27 | 28 | //make sure if it's a valid cell for DFS 29 | if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) { 30 | return 0; 31 | } 32 | 33 | // changing the cell value to 0 to avoid going in loop 34 | grid[i][j]=0; 35 | 36 | 37 | return 1 + 38 | dfs(grid, i-1, j) + 39 | dfs(grid, i, j+1) + 40 | dfs(grid, i+1, j) + 41 | dfs(grid, i, j-1); 42 | 43 | } 44 | ======= 45 | /////////////////////////////////////////// 46 | //695. Max Area of Island 47 | /////////////////////////////////////////// 48 | 49 | class Solution { 50 | public int maxAreaOfIsland(int[][] grid) { 51 | int maxArea= Integer.MIN_VALUE; 52 | 53 | for (int i = 0; i < grid.length; i++) { 54 | for (int j = 0; j < grid[0].length; j++) { 55 | if(grid[i][j]==1) { 56 | 57 | int area = dfs(grid, i, j); 58 | maxArea = Math.max(maxArea, area); 59 | } 60 | 61 | } 62 | } 63 | 64 | return maxArea==Integer.MIN_VALUE? 0 : maxArea; 65 | } 66 | 67 | 68 | 69 | public int dfs(int[][] grid, int i, int j) { 70 | 71 | //make sure if it's a valid cell for DFS 72 | if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) { 73 | return 0; 74 | } 75 | 76 | // changing the cell value to 0 to avoid going in loop 77 | grid[i][j]=0; 78 | 79 | 80 | return 1 + 81 | dfs(grid, i-1, j) + 82 | dfs(grid, i, j+1) + 83 | dfs(grid, i+1, j) + 84 | dfs(grid, i, j-1); 85 | 86 | } 87 | >>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 88 | } -------------------------------------------------------------------------------- /75_Sort_Colors.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //75. Sort Colors 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public void sortColors(int[] nums) { 8 | 9 | for(int i=0; i>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 48 | } -------------------------------------------------------------------------------- /796_Rotate_String.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //796. Rotate String 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | public boolean rotateString(String s, String goal) { 7 | 8 | if(s==null && goal==null) { 9 | return true; 10 | } 11 | 12 | if(s.length()!=goal.length()) { 13 | return false; 14 | } 15 | 16 | return (s+s).contains(goal)?true:false; 17 | } 18 | } -------------------------------------------------------------------------------- /797_All_Paths_From_Source_To_Target.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //797. All Paths From Source to Target 4 | /////////////////////////////////////////// 5 | 6 | class Solution { 7 | public List> allPathsSourceTarget(int[][] graph) { 8 | List> result = new ArrayList(); 9 | 10 | Map> g = buildGraph(graph); 11 | 12 | processGraph(0, graph.length-1, 0, g, new ArrayList(), result); 13 | 14 | return result; 15 | } 16 | 17 | private void processGraph(int source, int destination, int current, Map> graph, List list, List> result) { 18 | list.add(current); 19 | 20 | if(current == destination) { 21 | result.add(new ArrayList(list)); 22 | return; 23 | } 24 | 25 | List adjnodes = graph.get(current); 26 | 27 | 28 | if(adjnodes==null || adjnodes.size()==0) { 29 | return; 30 | } 31 | 32 | for(int i=0; i> buildGraph(int[][] graph) { 42 | Map> g = new HashMap(); 43 | 44 | for(int i=0; i> allPathsSourceTarget(int[][] graph) { 72 | List> result = new ArrayList(); 73 | 74 | Map> g = buildGraph(graph); 75 | 76 | processGraph(0, graph.length-1, 0, g, new ArrayList(), result); 77 | 78 | return result; 79 | } 80 | 81 | private void processGraph(int source, int destination, int current, Map> graph, List list, List> result) { 82 | list.add(current); 83 | 84 | if(current == destination) { 85 | result.add(new ArrayList(list)); 86 | return; 87 | } 88 | 89 | List adjnodes = graph.get(current); 90 | 91 | 92 | if(adjnodes==null || adjnodes.size()==0) { 93 | return; 94 | } 95 | 96 | for(int i=0; i> buildGraph(int[][] graph) { 106 | Map> g = new HashMap(); 107 | 108 | for(int i=0; i>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 130 | } -------------------------------------------------------------------------------- /827_Making_A_Large_Island.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 827 Making A Large Island - Hard Problem 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | private Map islandSizeMap; 8 | 9 | private final int[][] directions = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, }; 10 | 11 | private int color_id; 12 | 13 | public int largestIsland(int[][] grid) { 14 | 15 | color_id = 2; 16 | 17 | int rows = grid.length; 18 | 19 | int cols = grid[0].length; 20 | 21 | islandSizeMap = new HashMap<>(); 22 | 23 | int maxCount = 0; 24 | 25 | for (int i = 0; i < rows; i++) { 26 | for (int j = 0; j < cols; j++) { 27 | 28 | //we will mark this cell as part of an island and then we will do DFS in 4 29 | //directions to check what other cells are part of this island 30 | if (grid[i][j] == 1) { 31 | int size = markIslandDFS(grid, i, j, color_id); 32 | 33 | islandSizeMap.put(color_id, size); 34 | 35 | maxCount = Math.max(maxCount, size); 36 | 37 | color_id += 1; 38 | } 39 | } 40 | } 41 | 42 | for (int i = 0; i < rows; i++) { 43 | for (int j = 0; j < cols; j++) { 44 | 45 | if (grid[i][j] == 0) { 46 | maxCount = Math.max(maxCount, countIslands(grid, i, j) + 1); 47 | } 48 | } 49 | } 50 | 51 | return maxCount; 52 | } 53 | 54 | /* 55 | * we will go in 4 directions for the current cell if it's 0. And check if the current cell is surrounded by islands. 56 | * if it's surrounded by islands, then we will total sum of all the surrounded islands and add 1 to it (assuming that 57 | * we are converting current 0 cell to 1). Then we will return the size of the so called merged island. 58 | */ 59 | private int countIslands(int[][] grid, int row, int col) { 60 | 61 | Set visited = new HashSet<>(); 62 | 63 | int count = 0; 64 | 65 | for (int[] dir : directions) { 66 | 67 | int x = row + dir[0]; 68 | 69 | int y = col + dir[1]; 70 | 71 | if (insideBoundary(grid, x, y) && grid[x][y] > 1 && visited.add(grid[x][y])) { 72 | count += islandSizeMap.get(grid[x][y]); 73 | } 74 | } 75 | 76 | return count; 77 | } 78 | 79 | //we will mark this cell as part of an island and then we will do DFS in 4 80 | //directions to check what other cells are part of this island 81 | 82 | private int markIslandDFS(int[][] grid, int row, int col, int id) { 83 | 84 | if (!insideBoundary(grid, row, col) || grid[row][col] != 1) { 85 | return 0; 86 | } 87 | 88 | grid[row][col] = id; 89 | 90 | return 1 + markIslandDFS(grid, row + 1, col, id) + markIslandDFS(grid, row - 1, col, id) 91 | + markIslandDFS(grid, row, col + 1, id) + markIslandDFS(grid, row, col - 1, id); 92 | } 93 | 94 | private boolean insideBoundary(int[][] grid, int row, int col) { 95 | 96 | return row >= 0 && col >= 0 && row < grid.length && col < grid[0].length; 97 | 98 | } 99 | } -------------------------------------------------------------------------------- /86_Partition_List.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | /////////////////////////////////////////// 3 | //86. Partition List 4 | /////////////////////////////////////////// 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * public class ListNode { 9 | * int val; 10 | * ListNode next; 11 | * ListNode() {} 12 | * ListNode(int val) { this.val = val; } 13 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 14 | * } 15 | */ 16 | class Solution { 17 | public ListNode partition(ListNode head, int x) { 18 | 19 | ListNode curr=head; 20 | 21 | ListNode head1=new ListNode(); 22 | ListNode head2=new ListNode(); 23 | 24 | ListNode prev1=head1; 25 | ListNode prev2=head2; 26 | 27 | while(curr!=null) { 28 | if(curr.val>>>>>> c9a657537c4a10962f5426954865bacce8125cd6 102 | } -------------------------------------------------------------------------------- /916_Word_Subsets.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //916. Word Subsets 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | 8 | public List wordSubsets(String[] words1, String[] words2) { 9 | 10 | int[] combinedFreqArray = new int[26]; 11 | 12 | List result = new ArrayList<>(); 13 | 14 | for (String word : words2) { 15 | int[] chars = new int[26]; 16 | 17 | for (char c : word.toCharArray()) { 18 | chars[c - 'a']++; 19 | } 20 | 21 | for (int i = 0; i < 26; i++) { 22 | combinedFreqArray[i] = Math.max(chars[i], combinedFreqArray[i]); 23 | } 24 | } 25 | 26 | for (String word : words1) { 27 | int[] chars = new int[26]; 28 | 29 | for (char c : word.toCharArray()) { 30 | chars[c - 'a']++; 31 | } 32 | 33 | if (checkSubset(combinedFreqArray, chars)) { 34 | result.add(word); 35 | } 36 | } 37 | 38 | return result; 39 | } 40 | 41 | private boolean checkSubset(int[] combined, int[] chars) { 42 | for (int i = 0; i < 26; i++) { 43 | if (chars[i] < combined[i]) 44 | return false; 45 | } 46 | return true; 47 | } 48 | } -------------------------------------------------------------------------------- /949_Largest_Time_For_Given_Digits.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //1315. Sum of Nodes with Even-Valued Grandparent 3 | /////////////////////////////////////////// 4 | 5 | 6 | class Solution { 7 | public String largestTimeFromDigits(int[] arr) { 8 | 9 | Map freqmap = new HashMap(); 10 | 11 | for(int i = 0 ; i < arr.length ; i++) { 12 | if(freqmap.get(arr[i]) != null) { 13 | freqmap.put(arr[i], freqmap.get(arr[i]) + 1); 14 | } else { 15 | freqmap.put(arr[i],1); 16 | } 17 | 18 | } 19 | 20 | // start with maximum minutes and keep decrementing it to find maximum valid time that 21 | // can be formed with given digits 22 | int time = 23*60 + 59; 23 | 24 | String h = ""; 25 | String m = ""; 26 | 27 | boolean found=false; 28 | 29 | while(time>=0) { 30 | 31 | int hours = time/60; 32 | int minutes = time %60; 33 | 34 | 35 | h = String.valueOf(hours); 36 | m = String.valueOf(minutes); 37 | 38 | if(h.length()<2) { 39 | h="0"+h; 40 | } 41 | if(m.length()<2) { 42 | m="0"+m; 43 | } 44 | 45 | Map map = new HashMap(); 46 | 47 | 48 | for(int i = 0 ; i < h.length() ; i++) { 49 | int key = Integer.valueOf(h.charAt(i)-'0'); 50 | 51 | if(map.get(key) != null) { 52 | map.put(key, map.get(key) + 1); 53 | } else { 54 | map.put(key,1); 55 | } 56 | } 57 | 58 | for(int i = 0 ; i < m.length() ; i++) { 59 | int key = Integer.valueOf(m.charAt(i)-'0'); 60 | 61 | if(map.get(key) != null) { 62 | map.put(key, map.get(key) + 1); 63 | } else { 64 | map.put(key,1); 65 | } 66 | } 67 | 68 | 69 | 70 | if(freqmap.equals(map)) { 71 | found=true; 72 | break; 73 | } 74 | time--; 75 | } 76 | 77 | if(!found) { 78 | return ""; 79 | } 80 | 81 | String s = h+":"+m; 82 | return s; 83 | 84 | } 85 | } -------------------------------------------------------------------------------- /988_Smallest_String_Starting_From_Leaf.java: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | 988. Smallest String Starting From Leaf 3 | /************************************************************* 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | public String smallestFromLeaf(TreeNode root) { 22 | String smallest=""; 23 | 24 | ArrayList list=new ArrayList(); 25 | 26 | List> output=new ArrayList(); 27 | 28 | traverse(root, list, output); 29 | 30 | 31 | // process the paths from root to leaf to find the smallest String 32 | for(List l : output) { 33 | StringBuilder sb = new StringBuilder(); 34 | for(Integer n:l) { 35 | char c= (char)(n+'a'); 36 | sb.append(c); 37 | } 38 | String str = sb.reverse().toString(); 39 | 40 | if(smallest.equals("")) { 41 | smallest=str; 42 | } 43 | if(smallest.compareTo(str)>0) { 44 | smallest=str; 45 | } 46 | } 47 | 48 | return smallest; 49 | } 50 | 51 | private void traverse(TreeNode root, ArrayList list, List> output) { 52 | 53 | if(root == null) { 54 | return; 55 | } 56 | 57 | list.add(root.val); 58 | 59 | if(root.left==null && root.right==null) { 60 | output.add(new ArrayList(list)); 61 | return; 62 | } 63 | 64 | traverse(root.left, new ArrayList(list), output); 65 | traverse(root.right, new ArrayList(list), output); 66 | } 67 | } -------------------------------------------------------------------------------- /98_Validate_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //98. Validate Binary Search Tree 3 | /////////////////////////////////////////// 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * public class TreeNode { 8 | * int val; 9 | * TreeNode left; 10 | * TreeNode right; 11 | * TreeNode() {} 12 | * TreeNode(int val) { this.val = val; } 13 | * TreeNode(int val, TreeNode left, TreeNode right) { 14 | * this.val = val; 15 | * this.left = left; 16 | * this.right = right; 17 | * } 18 | * } 19 | */ 20 | class Solution { 21 | public boolean isValidBST(TreeNode root) { 22 | // return valid(root, null, null); 23 | 24 | if(root==null) { 25 | return true; 26 | } 27 | 28 | if(root.left==null && root.right==null) { 29 | return true; 30 | } 31 | 32 | return valid(root, Long.MIN_VALUE, Long.MAX_VALUE); 33 | } 34 | 35 | 36 | 37 | public boolean valid(TreeNode root, Long min, Long max) { 38 | if(root==null) { 39 | return true; 40 | } 41 | 42 | if(root.val <= min || root.val >= max) { 43 | return false; 44 | } 45 | 46 | boolean l = valid(root.left,min, (long)root.val ); 47 | boolean r = valid(root.right, (long)root.val , max); 48 | 49 | return l && r ; 50 | 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /994_Rotting_Oranges.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //994. Rotting Oranges 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | class Position { 8 | int i; 9 | int j; 10 | int state; 11 | 12 | public Position(int i, int j) { 13 | this.i = i; 14 | this.j = j; 15 | } 16 | 17 | public Position(int i, int j, int s) { 18 | this.i = i; 19 | this.j = j; 20 | this.state = s; 21 | } 22 | } 23 | 24 | 25 | public int orangesRotting(int[][] grid) { 26 | 27 | int total =0; 28 | int fresh = 0; 29 | 30 | Queue queue=new LinkedList(); 31 | 32 | for(int i=0;i=grid.length || j<0 || j>=grid[0].length ) || grid[i][j]!=1 ) { 95 | return false; 96 | } 97 | return true; 98 | } 99 | } -------------------------------------------------------------------------------- /Leetcode1024.java: -------------------------------------------------------------------------------- 1 | package com.exercises.general; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Leetcode 1024 Video Stitching 7 | * 8 | * Problem Statement: You are given a series of video clips from a sporting 9 | * event that lasted time seconds. These video clips can be overlapping with 10 | * each other and have varying lengths. 11 | * 12 | * Each video clip is described by an array clips where clips[i] = [starti, 13 | * endi] indicates that the ith clip started at starti and ended at endi. 14 | * 15 | * We can cut these clips into segments freely. 16 | * 17 | * For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7]. 18 | * Return the minimum number of clips needed so that we can cut the clips into 19 | * segments that cover the entire sporting event [0, time]. If the task is 20 | * impossible, return -1. 21 | * 22 | */ 23 | public class Leetcode1024 { 24 | 25 | public Leetcode1024() { 26 | 27 | } 28 | 29 | public int videoStitching(int[][] arr, int time) { 30 | 31 | Arrays.sort(arr, (a, b) -> a[0] - b[0]); 32 | // [[0, 2], [1, 9], [1, 5], [4, 6], [5, 9], [8, 10]] 33 | int start = 0; 34 | int end = 0; 35 | int result = 0; 36 | 37 | int i = 0; 38 | 39 | while (start < time) { 40 | 41 | for (; i < arr.length && arr[i][0] <= start; i++) { 42 | end = Math.max(end, arr[i][1]); 43 | } 44 | 45 | if (end == start) 46 | return -1; 47 | 48 | System.out.println("start:" + start + " end:" + end); 49 | 50 | start = end; 51 | 52 | result++; 53 | } 54 | 55 | return result; 56 | } 57 | 58 | public static void main(String[] args) { 59 | Leetcode1024 obj = new Leetcode1024(); 60 | 61 | // int[][] clips = { { 0, 2 }, { 4, 6 }, { 8, 10 }, { 1, 9 }, { 1, 5 }, { 5, 9 } 62 | // }; 63 | // int time = 10; 64 | 65 | int[][] clips = { { 0, 2 }, { 4, 6 }, { 2, 6 }, { 8, 10 }, { 10, 19 }, { 1, 5 }, { 18, 20 } }; 66 | int time = 20; 67 | 68 | // int[][] clips = { { 0, 2 }, { 4, 6 }, { 8, 10 }, {6,18},{ 10, 19 }, { 1, 5 }, 69 | // { 18, 20 } }; 70 | // int time = 20; 71 | 72 | int result = obj.videoStitching(clips, time); 73 | 74 | System.out.println(result); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /Leetcode1612.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | /** 4 | * LeetCode 1612. Check If Two Expression Trees are Equivalent 5 | * 6 | * A binary expression tree is a kind of binary tree used to represent 7 | * arithmetic expressions. Each node of a binary expression tree has either zero 8 | * or two children. Leaf nodes (nodes with 0 children) correspond to operands 9 | * (variables), and internal nodes (nodes with two children) correspond to the 10 | * operators. In this problem, we only consider the '+' operator (i.e. 11 | * addition). 12 | * 13 | * You are given the roots of two binary expression trees, root1 and root2. 14 | * Return true if the two binary expression trees are equivalent. Otherwise, 15 | * return false. 16 | * 17 | * Two binary expression trees are equivalent if they evaluate to the same value 18 | * regardless of what the variables are set to. 19 | * 20 | * 21 | */ 22 | public class Leetcode1612 { 23 | 24 | static class Node { 25 | public Node left; 26 | public Node right; 27 | public char val; 28 | 29 | public Node() { 30 | 31 | } 32 | 33 | public Node(char val) { 34 | this.left = this.right = null; 35 | this.val = val; 36 | } 37 | } 38 | 39 | public boolean checkEquivalence(Node root1, Node root2) { 40 | 41 | int[] count = new int[26]; 42 | 43 | // add 1 in count for character's (a-z) that are present 44 | dfs(root1, count, 1); 45 | 46 | // subtract 1 from count for character's (a-z) that are present 47 | dfs(root2, count, -1); 48 | 49 | for (int i = 0; i < count.length; i++) { 50 | if (count[i] != 0) { 51 | return false; 52 | } 53 | } 54 | 55 | return true; 56 | } 57 | 58 | private void dfs(Node cur, int[] count, int difference) { 59 | 60 | if (cur == null) { 61 | return; 62 | } 63 | 64 | if (cur.val != '+') { 65 | count[cur.val - 'a'] += difference; 66 | } 67 | 68 | dfs(cur.left, count, difference); 69 | 70 | dfs(cur.right, count, difference); 71 | } 72 | 73 | public static void main(String[] args) { 74 | Leetcode1612 obj = new Leetcode1612(); 75 | 76 | Node root1 = new Node(); 77 | root1.val = '+'; 78 | 79 | Node one_1 = new Node(); 80 | one_1.val = 'a'; 81 | 82 | Node one_2 = new Node(); 83 | one_2.val = '+'; 84 | 85 | Node one_3 = new Node(); 86 | one_3.val = 'b'; 87 | 88 | Node one_4 = new Node(); 89 | one_4.val = 'c'; 90 | 91 | root1.left = one_1; 92 | 93 | root1.right = one_2; 94 | 95 | root1.right.left = one_3; 96 | 97 | root1.right.right = one_4; 98 | 99 | Node root2 = new Node(); 100 | root2.val = '+'; 101 | 102 | Node two_1 = new Node(); 103 | two_1.val = 'a'; 104 | 105 | Node two_2 = new Node(); 106 | two_2.val = '+'; 107 | 108 | Node two_3 = new Node(); 109 | two_3.val = 'b'; 110 | 111 | 112 | 113 | root2.left = two_2; 114 | 115 | root2.right = two_1; 116 | 117 | root2.left.left = two_3; 118 | 119 | 120 | 121 | boolean result = obj.checkEquivalence(root1, root2); 122 | 123 | System.out.println(result); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /Leetcode_1604_Alert_Using_Same_Key_Card_Three_Or_More_Times_In_A_One_Hour_Period.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 1604 Alert Using Same Key Card Three or More Times in a One Hour Period 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public List alertNames(String[] keyName, String[] keyTime) { 8 | 9 | HashMap> map = new HashMap<>(); 10 | 11 | for (int i = 0; i < keyName.length; i++) { 12 | 13 | String s = keyName[i]; 14 | 15 | map.putIfAbsent(s, new ArrayList<>()); 16 | 17 | String[] arr = keyTime[i].split(":"); 18 | 19 | String time = arr[0] + arr[1]; 20 | 21 | map.get(s).add(Integer.parseInt(time)); 22 | } 23 | 24 | Set result = new TreeSet<>(); 25 | 26 | for (Map.Entry> e : map.entrySet()) { 27 | 28 | List list = new ArrayList<>(e.getValue()); 29 | 30 | Collections.sort(list); 31 | 32 | int count = 1; 33 | 34 | for (int i = 0; i < list.size(); i++) { 35 | for (int j = i + 1; j < list.size(); j++) { 36 | 37 | if (list.get(j) - list.get(i) <= 100) 38 | count++; 39 | else { 40 | count = 1; 41 | break; 42 | } 43 | 44 | if (count >= 3) 45 | break; 46 | } 47 | 48 | if (count >= 3) { 49 | result.add(e.getKey()); 50 | break; 51 | } 52 | } 53 | } 54 | 55 | return new ArrayList<>(result); 56 | 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /Leetcode_165_Compare_Version_Numbers.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 165 Compare Version Numbers 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public int compareVersion(String version1, String version2) { 8 | 9 | String arr1[] = version1.split("[.]"); 10 | 11 | String arr2[] = version2.split("[.]"); 12 | 13 | int N = Math.min(arr1.length, arr2.length); 14 | 15 | for (int i = 0; i < N; i++) { 16 | 17 | int a = Integer.parseInt(arr1[i]); 18 | 19 | int b = Integer.parseInt(arr2[i]); 20 | 21 | if (a > b) 22 | return 1; 23 | 24 | if (a < b) 25 | return -1; 26 | 27 | } 28 | 29 | if (arr1.length > arr2.length) { 30 | 31 | int i = N; 32 | 33 | while (i < arr1.length) { 34 | int a = Integer.parseInt(arr1[i]); 35 | if (a != 0) 36 | return 1; 37 | i++; 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | if (arr1.length < arr2.length) { 44 | 45 | int i = N; 46 | 47 | while (i < arr2.length) { 48 | int a = Integer.parseInt(arr2[i]); 49 | if (a != 0) 50 | return -1; 51 | i++; 52 | } 53 | return 0; 54 | 55 | } 56 | 57 | return 0; 58 | 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /Leetcode_2105.java: -------------------------------------------------------------------------------- 1 | package com.exercises.general; 2 | 3 | 4 | /** 5 | * Problem Statement: 6 | * 7 | * 8 | */ 9 | public class Leetcode_2105 { 10 | 11 | public Leetcode_2105() { 12 | 13 | } 14 | 15 | public int minimumRefill(int[] plant, int capacityA, int capacityB) { 16 | 17 | int lineLength = plant.length; 18 | 19 | int refill = 0; 20 | 21 | int a = capacityA; 22 | 23 | int b = capacityB; 24 | 25 | int i = 0; 26 | 27 | int j = lineLength - 1; 28 | 29 | while (i <= j) { 30 | 31 | //if both Alice and Bob reached a same plant 32 | if (i == j) { 33 | 34 | //who ever has more water available, will try to water the plant 35 | if (a < b) { 36 | if (b < plant[j]) { 37 | b = capacityB; 38 | refill++; 39 | } 40 | b -= plant[j]; 41 | } else { 42 | if (a < plant[i]) { 43 | a = capacityA; 44 | refill++; 45 | } 46 | a -= plant[i]; 47 | } 48 | } else { 49 | 50 | if (a < plant[i]) { 51 | a = capacityA; 52 | refill++; 53 | } 54 | 55 | //amount "a" will decrease by the amount of water you have given to the plant i 56 | a -= plant[i]; 57 | 58 | if (b < plant[j]) { 59 | b = capacityB; 60 | refill++; 61 | } 62 | //amount "b" will decrease by the amount of water you have given to the plant j 63 | b -= plant[j]; 64 | } 65 | 66 | //simulating that Alice is going from Left to Right 67 | i++; 68 | //simulating that Bob is going from Right to Left 69 | j--; 70 | } 71 | 72 | return refill; 73 | } 74 | 75 | public static void main(String[] args) { 76 | Leetcode_2105 obj = new Leetcode_2105(); 77 | 78 | int[] plant = { 2, 2, 3, 3 }; 79 | 80 | int capacityA = 3; 81 | 82 | int capacityB = 4; 83 | 84 | int result = obj.minimumRefill(plant, capacityA, capacityB); 85 | 86 | System.out.println(result); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /Leetcode_2125.java: -------------------------------------------------------------------------------- 1 | package com.codeforces1; 2 | 3 | public class Leetcode_2125 { 4 | 5 | public static int numberOfBeams(String[] bank) { 6 | int total = 0; 7 | int previous = 0; 8 | int current = 0; 9 | 10 | for (String next : bank) { 11 | 12 | current = 0; 13 | 14 | for (char nextChar : next.toCharArray()) { 15 | if (nextChar == '1') 16 | current++; 17 | } 18 | 19 | total += (current * previous); 20 | 21 | if (current != 0) 22 | previous = current; 23 | } 24 | 25 | return total; 26 | } 27 | 28 | public static void main(String[] args) throws java.lang.Exception { 29 | String[] bank = { "011001", "000000", "010100", "001000" }; 30 | int result = numberOfBeams(bank); 31 | 32 | System.out.println(result); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Leetcode_2130.java: -------------------------------------------------------------------------------- 1 | package com.codeforces1; 2 | 3 | public class Leetcode_2130 { 4 | 5 | static class ListNode { 6 | int val; 7 | ListNode next; 8 | 9 | ListNode() { 10 | } 11 | 12 | ListNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | ListNode(int val, ListNode next) { 17 | this.val = val; 18 | this.next = next; 19 | } 20 | } 21 | 22 | public static int size(ListNode head) { 23 | if (head == null) { 24 | return 0; 25 | } 26 | return 1 + size(head.next); 27 | } 28 | 29 | public static ListNode reverse(ListNode head) { 30 | if (head == null) { 31 | return null; 32 | } 33 | ListNode a = head; 34 | ListNode b = head.next; 35 | if (b == null) { 36 | return head; 37 | } 38 | ListNode c = b.next; 39 | a.next = null; 40 | while (c != null) { 41 | b.next = a; 42 | a = b; 43 | b = c; 44 | c = c.next; 45 | } 46 | b.next = a; 47 | return b; 48 | } 49 | 50 | public static int pairSum(ListNode head) { 51 | int n = size(head); 52 | int p = n / 2; 53 | // reverse second half of list 54 | ListNode curr = head; 55 | for (int i = 0; i < p - 1; i++) { 56 | curr = curr.next; 57 | } 58 | ListNode x = curr.next; 59 | curr.next = null; 60 | ListNode otherHead = reverse(x); 61 | // compute the max sum 62 | int max = 0; 63 | while (head != null) { 64 | int sum = head.val + otherHead.val; 65 | if (sum > max) { 66 | max = sum; 67 | } 68 | head = head.next; 69 | otherHead = otherHead.next; 70 | } 71 | return max; 72 | } 73 | 74 | public static void main(String[] args) throws java.lang.Exception { 75 | 76 | ListNode head = new ListNode(1); 77 | ListNode second = new ListNode(2); 78 | ListNode third = new ListNode(3); 79 | ListNode fourth = new ListNode(4); 80 | 81 | head.next = second; 82 | second.next = third; 83 | third.next = fourth; 84 | 85 | int sum = pairSum(head); 86 | 87 | System.out.println("sum:" + sum); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /Leetcode_2131.java: -------------------------------------------------------------------------------- 1 | package com.codeforces1; 2 | 3 | import java.util.HashMap; 4 | 5 | public class Leetcode_2131 { 6 | 7 | public static int longestPalindrome(String[] words) { 8 | HashMap map = new HashMap<>(); 9 | 10 | int max = 0; 11 | 12 | for (int i = 0; i < words.length; i++) { 13 | 14 | String reversed = words[i].charAt(1) + "" + words[i].charAt(0); 15 | 16 | if (map.containsKey(reversed)) { 17 | 18 | max += 4; 19 | 20 | // decrease count 21 | map.put(reversed, map.get(reversed) - 1); 22 | 23 | if (map.get(reversed) == 0) 24 | map.remove(reversed); 25 | 26 | continue; 27 | } 28 | 29 | map.put(words[i], map.getOrDefault(words[i], 0) + 1); 30 | } 31 | 32 | for (String k : map.keySet()) { 33 | // handling words like "dd" where both characters are same. this can be put in 34 | // the middle 35 | // when forming a palindrome. 36 | // e.g. abddba 37 | 38 | if (map.get(k) == 1 && (k.charAt(1) + "" + k.charAt(0)).equals(k)) { 39 | max += 2; 40 | break; 41 | } 42 | } 43 | 44 | return max; 45 | } 46 | 47 | public static void main(String[] args) throws java.lang.Exception { 48 | String[] words = { "ab", "ty", "yt", "lc", "cl", "ab", "dd" }; 49 | int result = longestPalindrome(words); 50 | 51 | System.out.println(result); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Leetcode_2133_Check_If_Every_Row_And_Column_Contains_All_Numbers.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 2133 Check if Every Row and Column Contains All Numbers 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public boolean checkValid(int[][] matrix) { 8 | 9 | int n = matrix.length; 10 | 11 | for (int r = 0; r < n; ++r) { 12 | 13 | Set row = new HashSet<>(); 14 | Set col = new HashSet<>(); 15 | 16 | for (int c = 0; c < n; ++c) { 17 | row.add(matrix[r][c]); 18 | col.add(matrix[c][r]); 19 | } 20 | 21 | if (row.size() < n || col.size() < n) { 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /Leetcode_2135.java: -------------------------------------------------------------------------------- 1 | package com.codeforces1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | public class Leetcode_2135 { 10 | 11 | public static int wordCount(String[] startWords, String[] targetWords) { 12 | HashSet set = new HashSet<>(); 13 | 14 | for (String word : startWords) { 15 | char[] c = word.toCharArray(); 16 | 17 | Arrays.sort(c); 18 | 19 | word = ""; 20 | 21 | for (char ch : c) 22 | word += ch; 23 | 24 | set.add(word); 25 | } 26 | 27 | int count = 0; 28 | 29 | for (String s : targetWords) { 30 | char[] c = s.toCharArray(); 31 | 32 | Arrays.sort(c); 33 | 34 | for (int i = 0; i < c.length; i++) { 35 | String str = ""; 36 | 37 | for (int j = 0; j < i; j++) 38 | str += c[j]; 39 | 40 | for (int j = i + 1; j < c.length; j++) 41 | str += c[j]; 42 | 43 | //System.out.println("checking:" + str); 44 | 45 | if (set.contains(str)) { 46 | 47 | //System.out.println("found"); 48 | count++; 49 | break; 50 | } 51 | } 52 | } 53 | return count; 54 | } 55 | 56 | public static void main(String[] args) throws java.lang.Exception { 57 | String[] startWords = { "ant", "act", "tack" }; 58 | String[] targetWords = { "tack", "act", "acti" }; 59 | 60 | int result = wordCount(startWords, targetWords); 61 | 62 | System.out.println(result); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Leetcode_2135_Count_Words_Obtained_After_Adding_A_Letter.java: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | //Leetcode 2135 Count Words Obtained After Adding a Letter 3 | /////////////////////////////////////////// 4 | 5 | class Solution { 6 | 7 | public int wordCount(String[] startWords, String[] targetWords) { 8 | 9 | HashSet set = new HashSet<>(); 10 | 11 | for(String word: startWords){ 12 | char[] c = word.toCharArray(); 13 | 14 | Arrays.sort(c); 15 | 16 | word = ""; 17 | 18 | for(char ch: c) 19 | word += ch; 20 | 21 | set.add(word); 22 | } 23 | 24 | int count = 0; 25 | 26 | for(String s: targetWords){ 27 | char[] c = s.toCharArray(); 28 | 29 | Arrays.sort(c); 30 | 31 | for(int i=0; i findLonely(int[] nums) { 8 | 9 | Map map = new HashMap<>(); 10 | 11 | for (int num : nums) { 12 | map.put(num, map.getOrDefault(num, 0) + 1); 13 | } 14 | 15 | List ans = new ArrayList<>(); 16 | 17 | for (int num : nums) { 18 | // if map does not contains (num+1) and (num-1) number, num is a lonely number. 19 | // then we will add it to ans list 20 | 21 | if (map.get(num) == 1 && !map.containsKey(num + 1) && !map.containsKey(num - 1)) { 22 | ans.add(num); 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Leetcode_802.java: -------------------------------------------------------------------------------- 1 | package com.exercises.general; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | public class Leetcode_802 { 11 | 12 | public List eventualSafeNodes(int[][] graph) { 13 | Map memoizationMap = new HashMap<>(); 14 | for (int i = 0; i < graph.length; i++) { 15 | //terminal node 16 | if (graph[i].length == 0) 17 | memoizationMap.put(i, true); 18 | } 19 | 20 | for (int i = 0; i < graph.length; i++) { 21 | boolean result = dfs(i, graph, new HashSet(), memoizationMap); 22 | memoizationMap.put(i, result); 23 | } 24 | 25 | List list = new ArrayList<>(); 26 | 27 | for (int i = 0; i < graph.length; i++) { 28 | if (memoizationMap.get(i)) 29 | list.add(i); 30 | } 31 | 32 | return list; 33 | 34 | } 35 | 36 | public boolean dfs(int curr, int[][] graph, Set visited, Map memoizationMap) { 37 | System.out.println("checking :"+curr); 38 | if (memoizationMap.containsKey(curr)) { 39 | return memoizationMap.get(curr); 40 | } 41 | 42 | // if node is already visited, it means multiple possible paths in that node, so 43 | // will return false 44 | if (visited.contains(curr)) { 45 | return false; 46 | } 47 | 48 | visited.add(curr); 49 | 50 | for (int i : graph[curr]) { 51 | boolean flag=dfs(i, graph, visited, memoizationMap); 52 | 53 | if (flag) 54 | memoizationMap.put(i, true); 55 | else 56 | return false; 57 | } 58 | 59 | // backtracking 60 | visited.remove(curr); 61 | 62 | return true; 63 | } 64 | 65 | 66 | public int minSwap(int[] nums1, int[] nums2) { 67 | int len = nums1.length; 68 | int[][] dp = new int[2][len+1]; 69 | for(int i = 0; i < 2; i++) 70 | { 71 | dp[i][0] = 0; 72 | dp[i][1] = i; 73 | } 74 | 75 | for(int i = 2; i <= len; i++) 76 | { 77 | int pre1 = nums1[i-2]; 78 | int pre2 = nums2[i-2]; 79 | int cur1 = nums1[i-1]; 80 | int cur2 = nums2[i-1]; 81 | dp[0][i] = Integer.MAX_VALUE; 82 | dp[1][i] = Integer.MAX_VALUE; 83 | 84 | System.out.println(pre1+" "+pre2+" "+cur1+" "+cur2); 85 | 86 | if(cur1>pre1 && cur2 > pre2) 87 | { 88 | dp[0][i] = Math.min(dp[0][i], dp[0][i-1]); 89 | dp[1][i] = Math.min(dp[1][i], dp[1][i-1]+1); 90 | } 91 | 92 | if(cur1 > pre2 && cur2 > pre1) 93 | { 94 | dp[0][i] = Math.min(dp[0][i], dp[1][i-1]); 95 | dp[1][i] = Math.min(dp[1][i], dp[0][i-1]+1); 96 | } 97 | } 98 | 99 | return Math.min(dp[0][len], dp[1][len]); 100 | } 101 | 102 | public static void main(String[] args) { 103 | Leetcode_802 obj = new Leetcode_802(); 104 | 105 | int[][] graph = { { 1, 2 }, { 2, 3 }, { 5 }, { 0 }, { 5 }, {}, {} }; 106 | List r = obj.eventualSafeNodes(graph); 107 | System.out.println(r); 108 | 109 | int[] nums1= {0,3,5,8,9}; 110 | int[] nums2= {2,1,4,6,9}; 111 | int result=obj.minSwap(nums1, nums2); 112 | System.out.println(result); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /binary_tree_top_bottom_view/BinaryTreeBottomView.java: -------------------------------------------------------------------------------- 1 | package com.exercises.interview; 2 | 3 | 4 | import java.util.LinkedList; 5 | import java.util.Map; 6 | import java.util.Map.Entry; 7 | import java.util.Queue; 8 | import java.util.TreeMap; 9 | 10 | class Node { 11 | int val; 12 | Node left, right; 13 | 14 | public Node(int val) { 15 | this.val = val; 16 | left = right = null; 17 | } 18 | } 19 | 20 | //class of binary tree 21 | class BinaryTreeBottomView { 22 | Node root; 23 | 24 | public BinaryTreeBottomView() { 25 | root = null; 26 | } 27 | 28 | // top view of binary tree 29 | private void showTopView(Node root) { 30 | 31 | class NodeDistance { 32 | Node node; 33 | int distance; 34 | 35 | NodeDistance(Node node, int hd) { 36 | this.node = node; 37 | this.distance = hd; 38 | } 39 | } 40 | 41 | 42 | Queue queue = new LinkedList(); 43 | 44 | Map map = new TreeMap(); 45 | 46 | if (root == null) { 47 | return; 48 | } else { 49 | queue.add(new NodeDistance(root, 0)); 50 | } 51 | 52 | 53 | // traverse the binary tree and add the nodes into the map only if there is no entry present. this way we can 54 | // capture the top view of the tree 55 | while (!queue.isEmpty()) { 56 | 57 | NodeDistance tmpNode = queue.poll(); 58 | 59 | map.put(tmpNode.distance, tmpNode.node); 60 | 61 | 62 | // if we are going to the left, we will subtract 1 from the distance 63 | if (tmpNode.node.left != null) { 64 | queue.add(new NodeDistance(tmpNode.node.left, tmpNode.distance - 1)); 65 | } 66 | 67 | // if we are going to the right, we will add 1 to the distance 68 | if (tmpNode.node.right != null) { 69 | queue.add(new NodeDistance(tmpNode.node.right, tmpNode.distance + 1)); 70 | } 71 | } 72 | 73 | for (Entry entry : map.entrySet()) { 74 | System.out.print(entry.getValue().val+" "); 75 | } 76 | } 77 | 78 | public static void main(String[] args) { 79 | 80 | BinaryTreeBottomView tree = new BinaryTreeBottomView(); 81 | tree.root = new Node(200); 82 | tree.root.left = new Node(40); 83 | tree.root.right = new Node(400); 84 | tree.root.left.right = new Node(20); 85 | tree.root.left.right.right = new Node(80); 86 | tree.root.left.right.right.left = new Node(10); 87 | tree.root.left.right.right.right = new Node(60); 88 | System.out.println("bottom view :"); 89 | tree.showTopView(tree.root); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /binary_tree_top_bottom_view/BinaryTreeTopView.java: -------------------------------------------------------------------------------- 1 | package com.example.codility.test.data_structure8; 2 | 3 | 4 | import java.util.LinkedList; 5 | import java.util.Map; 6 | import java.util.Map.Entry; 7 | import java.util.Queue; 8 | import java.util.TreeMap; 9 | 10 | class Node { 11 | int val; 12 | Node left, right; 13 | 14 | public Node(int val) { 15 | this.val = val; 16 | left = right = null; 17 | } 18 | } 19 | 20 | //class of binary tree 21 | class BinaryTreeTopView { 22 | Node root; 23 | 24 | public BinaryTreeTopView() { 25 | root = null; 26 | } 27 | 28 | // top view of binary tree 29 | private void showTopView(Node root) { 30 | 31 | class NodeDistance { 32 | Node node; 33 | int distance; 34 | 35 | NodeDistance(Node node, int hd) { 36 | this.node = node; 37 | this.distance = hd; 38 | } 39 | } 40 | 41 | 42 | Queue queue = new LinkedList(); 43 | 44 | Map map = new TreeMap(); 45 | 46 | if (root == null) { 47 | return; 48 | } else { 49 | queue.add(new NodeDistance(root, 0)); 50 | } 51 | 52 | 53 | // traverse the binary tree and add the nodes into the map only if there is no entry present. this way we can 54 | // capture the top view of the tree 55 | while (!queue.isEmpty()) { 56 | 57 | NodeDistance tmp = queue.poll(); 58 | 59 | if (!map.containsKey(tmp.distance)) { 60 | map.put(tmp.distance, tmp.node); 61 | } 62 | 63 | 64 | // if we are going to the left, we will subtract 1 from the distance 65 | if (tmp.node.left != null) { 66 | 67 | queue.add(new NodeDistance(tmp.node.left, tmp.distance - 1)); 68 | } 69 | 70 | // if we are going to the right, we will add 1 to the distance 71 | if (tmp.node.right != null) { 72 | queue.add(new NodeDistance(tmp.node.right, tmp.distance + 1)); 73 | } 74 | } 75 | 76 | for (Entry entry : map.entrySet()) { 77 | System.out.print(entry.getValue().val+" "); 78 | } 79 | } 80 | 81 | public static void main(String[] args) { 82 | 83 | BinaryTreeTopView tree = new BinaryTreeTopView(); 84 | tree.root = new Node(200); 85 | tree.root.left = new Node(40); 86 | tree.root.right = new Node(400); 87 | tree.root.left.right = new Node(20); 88 | tree.root.left.right.right = new Node(80); 89 | tree.root.left.right.right.left = new Node(10); 90 | tree.root.left.right.right.right = new Node(60); 91 | System.out.println("Top view :"); 92 | tree.showTopView(tree.root); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /binary_tree_top_bottom_view/btree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maksrane100/leetcode_solutions/18c306c80a61c9f93f858b2dc142b873d4007b00/binary_tree_top_bottom_view/btree.png -------------------------------------------------------------------------------- /collections_java.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maksrane100/leetcode_solutions/18c306c80a61c9f93f858b2dc142b873d4007b00/collections_java.docx -------------------------------------------------------------------------------- /dynamic_programming/LCS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maksrane100/leetcode_solutions/18c306c80a61c9f93f858b2dc142b873d4007b00/dynamic_programming/LCS.png -------------------------------------------------------------------------------- /dynamic_programming/LongestCommonSubstring.java: -------------------------------------------------------------------------------- 1 | package com.exercises.interview; 2 | 3 | /* 4 | * Longest common substring. 5 | * 6 | * Uses tabulation of dynamic programming. 7 | */ 8 | public class LongestCommonSubstring { 9 | 10 | public static String findLCS(String S1, String S2, int m, int n) 11 | { 12 | int maxLength = 0; 13 | int endIndex = m; // end index of LCS in S1 14 | 15 | int[][] DP = new int[m + 1][n + 1]; 16 | 17 | // bottom-up fashion dynamic programming 18 | for (int i = 1; i <= m; i++) 19 | { 20 | for (int j = 1; j <= n; j++) 21 | { 22 | // if the current character of S1 and S2 matches 23 | if (S1.charAt(i - 1) == S2.charAt(j - 1)) 24 | { 25 | DP[i][j] = DP[i - 1][j - 1] + 1; 26 | 27 | // update the maximum length and ending index 28 | if (DP[i][j] > maxLength) 29 | { 30 | maxLength = DP[i][j]; 31 | endIndex = i; 32 | } 33 | } 34 | } 35 | } 36 | 37 | return S1.substring(endIndex - maxLength, endIndex); 38 | } 39 | 40 | public static void main(String[] args) 41 | { 42 | String string1 = "SOMANYBEUTIFULFLOWERS"; 43 | String string2 = "MANYFLOWERS"; 44 | 45 | int m = string1.length(); 46 | int n = string2.length(); 47 | 48 | String lcsString = findLCS(string1, string2, m, n); 49 | 50 | System.out.print("Longest common substring : " 51 | + lcsString); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /dynamic_programming/MinimumMatrixCost.java: -------------------------------------------------------------------------------- 1 | package com.exercises.interview; 2 | 3 | /** 4 | * 5 | * Find cost to traverse from top left of the matrix to bottom right of the matrix. 6 | * This uses dynamic programming. 7 | * 8 | */ 9 | public class MinimumMatrixCost { 10 | 11 | public static int calculateMinimumCostUsingDP(int[][] matrix) { 12 | int ROWS = matrix.length; 13 | int COLS = matrix[0].length; 14 | 15 | int[][] DP = new int[ROWS][COLS]; 16 | 17 | // bottom-up fashion 18 | for (int i = 0; i < ROWS; i++) { 19 | for (int j = 0; j < COLS; j++) { 20 | DP[i][j] = matrix[i][j]; 21 | 22 | if (i == 0 && j > 0) { 23 | DP[0][j] += DP[0][j - 1]; 24 | } 25 | 26 | else if (j == 0 && i > 0) { 27 | DP[i][0] += DP[i - 1][0]; 28 | } 29 | 30 | else if (i > 0 && j > 0) { 31 | DP[i][j] += Integer.min(DP[i - 1][j], DP[i][j - 1]); 32 | } 33 | } 34 | } 35 | 36 | return DP[ROWS - 1][COLS - 1]; 37 | } 38 | 39 | public static void main(String[] args) { 40 | int[][] arr = { 41 | { 2, 4, 6, 10, 8 }, 42 | { 1, 4, 14, 20, 2 }, 43 | { 2, 3, 16, 30, 3 }, 44 | { 8, 4, 4, 40, 4 }, 45 | { 12, 10, 6, 2, 5 }, 46 | 47 | }; 48 | 49 | System.out.print("Minimum cost is " + calculateMinimumCostUsingDP(arr)); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /dynamic_programming/MinimumMatrixCost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maksrane100/leetcode_solutions/18c306c80a61c9f93f858b2dc142b873d4007b00/dynamic_programming/MinimumMatrixCost.png -------------------------------------------------------------------------------- /interview_questions.txt: -------------------------------------------------------------------------------- 1 | 1) How Set is implemented in Java? 2 | 3 | 2) What classes you used from Java's Collection framework? 4 | 5 | 3) Which Java design patterns you have used and where did you use them? 6 | 7 | 4) What is difference between PATH and CLASSPATH ? 8 | 9 | -------------------------------------------------------------------------------- /lag_function_mysql.sql: -------------------------------------------------------------------------------- 1 | -- Find sum, average, min, max salary per department 2 | 3 | SELECT first_name, Salary, department_id, 4 | COUNT(department_id) OVER(PARTITION BY department_id) AS TotalEmployees, 5 | SUM(Salary) OVER(PARTITION BY department_id) AS TotalSalary, 6 | AVG(Salary) OVER(PARTITION BY department_id) AS AverageSalary, 7 | MIN(Salary) OVER(PARTITION BY department_id) AS MinSalary, 8 | MAX(Salary) OVER(PARTITION BY department_id) AS MaxSalary 9 | FROM 10 | employees; 11 | 12 | SELECT 13 | employee_id, 14 | first_name, 15 | last_name, 16 | email, 17 | job_id, 18 | hire_date, 19 | LAG(hire_date,1) OVER ( 20 | PARTITION BY job_id 21 | ORDER BY hire_date ) as PreviousHireDate 22 | FROM 23 | employees; 24 | 25 | SELECT 26 | employee_id, 27 | first_name, 28 | last_name, 29 | email, 30 | job_id, 31 | hire_date, 32 | salary, 33 | LAG(salary,1) OVER ( 34 | PARTITION BY job_id 35 | ORDER BY salary ) as PreviousSalary 36 | FROM 37 | employees; -------------------------------------------------------------------------------- /lead_function_mysql.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | employee_id, 3 | first_name, 4 | last_name, 5 | email, 6 | job_id, 7 | hire_date, 8 | avg(salary) OVER ( 9 | PARTITION BY job_id) 10 | FROM 11 | employees; 12 | 13 | SELECT 14 | employee_id, 15 | first_name, 16 | last_name, 17 | email, 18 | job_id, 19 | hire_date, 20 | LEAD(hire_date,2) OVER ( 21 | PARTITION BY job_id 22 | ORDER BY hire_date ) as NextHireDate 23 | FROM 24 | employees; 25 | 26 | SELECT 27 | employee_id, 28 | first_name, 29 | last_name, 30 | email, 31 | job_id, 32 | hire_date, 33 | salary, 34 | LEAD(salary,1) OVER ( 35 | PARTITION BY job_id 36 | ORDER BY salary ) as NextSalary 37 | FROM 38 | employees; -------------------------------------------------------------------------------- /unix_commands.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maksrane100/leetcode_solutions/18c306c80a61c9f93f858b2dc142b873d4007b00/unix_commands.docx -------------------------------------------------------------------------------- /unix_commands_for_interview: -------------------------------------------------------------------------------- 1 | Top Interview Unix Commands (For Java Developers) 2 | Command 3 | 4 | chmod 5 | chmod is the command and system call used to change the access permissions of file system. 6 | 7 | 4(r) + 2(w) + 1(x) 8 | 9 | Each group has three permissions rwx stands for read, write and execute and they are written as user_group_others. 10 | 11 | For example,754 would allow: 12 | • "read" (4), "write" (2), and "execute" (1) for the User class i.e. 7 (4+2+1). 13 | • "read" (4) and "execute" (1) for the Group class i.e. 5 (4+1). 14 | • Only "read" (4) for the Others class. 15 | 16 | 17 | ls 18 | ls -ltr 19 | This combination will give size, long list and sort files from the time of creation/modify. 20 | 21 | grep 22 | grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. 23 | 24 | grep “^Processing” file1 25 | 26 | Match all lines that start with “Processing” 27 | 28 | kill 29 | Used to kill or terminate a process 30 | 31 | kill 32 | find 33 |  find is a command-line utility that locates files based on some user-specified criteria 34 | 35 | find ./tmp -name sample.txt 36 | 37 | search for file named sample.txt under ./tmp directory 38 | 39 | pwd 40 | writes the full pathname of the current working directory to the standard output 41 | mv 42 | mv is a Unix command that moves one or more files or directories from one place to another 43 | rm 44 | rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories 45 | rmdir 46 | rmdir is a command which will remove an empty directory 47 | echo 48 | echo is a command that outputs the strings it is being passed as arguments 49 | cat 50 | cat is a standard Unix utility that reads files sequentially, writing them to standard output 51 | whoami 52 | prints the effective username of the current user 53 | cron 54 | time-based job schedule 55 | ping 56 | utility used to test the reachability of a host on an Internet 57 | head 58 | Read first few lines from top of the file 59 | tail 60 | Read few lines from the end of the file 61 | 62 | 63 | • How to find running “java” processes on unix: 64 | 65 | ps -ef | grep java 66 | 67 | • In a file word "Error" is appearing many times. How will you count number? 68 | 69 | grep -c "Error" filename 70 | 71 | 72 | 73 | • How do you set environment variable which will be accessible form sub shell? 74 | 75 | By using export command, for example export filelocation=/tmp/scripts will be available on all sub shell. 76 | 77 | • find which operating system your system is running on 78 | 79 | uname 80 | 81 | • How to find all text file which contains word Exception 82 | 83 | find. –name "*.txt" | grep "Exception" 84 | 85 | • How to check if a particular process is listening on a particular port on remote host? 86 | 87 | telnet hostname port 88 | 89 | 90 | • How do you copy file from one host to other server 91 | 92 | using scp or sftp command 93 | 94 | • How do you find which process is consuming how much CPU? 95 | 96 | Using top command 97 | 98 | • How do you check how much space left in current drive 99 | 100 | using df 101 | 102 | • In VI editor, how can you go to the end of the file without using arrow keys: 103 | 104 | Shift+G 105 | --------------------------------------------------------------------------------