## Posts

Showing posts from September, 2020

### LeetCode - Crawler Log Folder Solution - The Coding Shala

Home >> LeetCode >> Crawler Log Folder  In this post, we will learn how to solve LeetCode's Crawler Log Folder problem and will implement Crawler Log Folder Java Solution. Crawler Log Folder Problem The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: "../": Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder). "./": Remain in the same folder. "x/": Move to the child folder named x (This folder is guaranteed to always exist). You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step. The file system starts in the main folder, then the operations in logs are performed. Return the minimum number of operations needed to go back to the main folder after the change folder operations. Input: logs = ["d1/","d2/","../","d2

### LeetCode - Buddy Strings Solution - The Coding Shala

Home >> LeetCode >> Buddy Strings  In this post, we will learn how to solve LeetCode's Buddy Strings problem and will implement its solution in Java language. Buddy Strings Problem Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: Input: A = "ab", B = "ba" Output: true Example 2: Input: A = "ab", B = "ab" Output: false Example 3: Input: A = "aa", B = "aa" Output: true Example 4: Input: A = "aaaaaaabc", B = "aaaaaaacb" Output: true Example 5: Input: A = "", B = "aa" Output: false Practice this problem in LeetCode: Click Here. Buddy Strings Java Solution Approach 1: Here, we need to check three things: First, if both strings have different length then there is no possible swaps. if both strings are the same then check if there are duplicate then we can swap duplicate char t

### LeetCode - Friends Of Appropriate Ages Solution - The Coding Shala

Home >> LeetCode >> Friend of Appropriate Ages  In this post, we will see how to solve LeetCode's Friends Of Appropriate Ages Problem and its solution in Java. Friends Of Appropriate Ages Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.  Person A will NOT friend request person B (B != A) if any of the following conditions are true: age[B] <= 0.5 * age[A] + 7 age[B] > age[A] age[B] > 100 && age[A] < 100 Otherwise, A will friend request B. Note that if A requests B, B does not necessarily request A.  Also, people will not friend request themselves. How many total friend requests are made? Example 1: Input: [16,16] Output: 2 Explanation: 2 people friend request each other. Example 2: Input: [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17. Example 3: Input: [20,30,100,110,120] Output: 3 Explanation: Friend requests are made 110 -> 100, 120 ->

### Java Program to find Sum of digits of a Number - The Coding Shala

Home >> Java Programs >> Sum of digits of a number In this post, we will learn how to write a Java program to find the sum of digits of a given number. Sum of Digits of a Number Java Program We will take a reminder of 10 that will give us the last digits of the given number. Add this digit to sum and divide the number by 10 and repeat this step until input number is greater than 0. Example 1: Input: 476 Output: 17 Explanation: 4 + 7 + 6  = 17. Java Program:  class Main { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); // put your code here int num = scanner . nextInt (); int ans = 0 ; while ( num > 0 ) { ans += num % 10 ; num /= 10 ; } System . out . println ( ans ); } } Other Posts You May Like Add Two Numbers Two Sum Problem Add one to Number Plus One Minimum Size subarray sum Please lea

### LeetCode - Shuffle the Array Solution - The Coding Shala

Home >> LeetCode >> Shuffle the Array  In this post, we will learn how to solve LeetCode's Shuffle the Array problem and will implement its solution in Java. Shuffle the Array Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn]. Example 1: Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7]  Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. Example 2: Input: nums = [1,2,3,4,4,3,2,1], n = 4 Output: [1,4,2,3,3,2,4,1] Practice this problem on LeetCode: Click Here. Shuffle the Array Java Solution Approach 1: Using a new Array. Space complexity: O(n). Java Program:  class Solution { public int [] shuffle ( int [] nums , int n ) { int [] ans = new int [ 2 * n ]; int j = 0 ; for ( int i = 0 ; i < 2 * n ; i = i + 2 ){ ans [ i ] = nums [ j ]; ans [ i + 1 ] =

### LeetCode - Number of Good Pairs Solution - The Coding Shala

Home >> LeetCode >> Number of Good Pairs In this post, we will learn how to solve LeetCode's Number of Good Pairs problem and will implement its solution in Java. Number of Good Pairs Given an array of integers nums. A pair (i,j) is called good if nums[i] == nums[j] and i < j. Return the number of good pairs. Example 1: Input: nums = [1,2,3,1,1,3] Output: 4 Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. Example 2: Input: nums = [1,1,1,1] Output: 6 Explanation: Each pair in the array are good. Practice this problem on LeetCode: (Click Here). Number of Good Pairs Java Solution Approach 1: Using two loops. Time Complexity: O(n^2). Java Program:  class Solution { public int numIdenticalPairs ( int [] nums ) { int ans = 0 ; for ( int i = 0 ; i < nums . length - 1 ; i ++) { for ( int j = i + 1 ; j < nums . length ; j ++) { if ( nums [ i ] == nums [ j ]) ans

### LeetCode - Kids With the Greatest Number of Candies Solution - The Coding Shala

Home >> LeetCode >> Kids with the Greatest Number of Candies   In this post, we will learn how to solve LeetCode's Kids With the Greatest Number of Candies problem and will implement its solution in Java. Kids With the Greatest Number of Candies Given the array candies and the integer extra candies, where candies[i] represents the number of candies that the ith kid has. For each kid check if there is a way to distribute extra candies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies. Example 1: Input: candies = [2,3,5,1,3], extraCandies = 3 Output: [true,true,true,false,true]  Explanation: Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies  Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. Kid 3 has 5 candies and this is already the greatest number