Posts

DSA Array | Valid Anagram

Problem Statement Write a function that takes two strings, s and t, as input and determines whether they are anagrams of each other. Return true if the two strings are anagrams and false otherwise. An anagram is defined as a string formed by rearranging the characters of another string, using all the original characters exactly once. Example 1: Input: s = "listen", t = "silent" Output: true Explanation: Both strings contain the same characters ('l', 'i', 's', 't', 'e', 'n') in different orders. Example 2: Input: s = "hello", t = "world" Output: false Explanation: The characters in s and t do not match exactly. Code: class Solution { public boolean isAnagram(String s, String t) { Map <Character, Integer> map = new HashMap <> (); int n1 = s.length(); ...

DSA Blind 75 | Problems and solutions

75 DSA problems topicwise Given below is a list of popular DSA problems topicwise with solutions. Arrays Contains duplicate Valid Anagram more to come..

DSA Array | Contains duplicate

Problem Statement You are given an array of integers, nums . Your task is to determine if the array contains any duplicate values. Return: true if any value appears more than once in the array. false if all values in the array are unique. Examples Example 1: Input: nums = [1, 2, 3, 1] Output: true   Explanation: The value 1 appears twice in the array. Example 2: Input: nums = [1, 2, 3, 4] Output: false   Explanation: All values in the array are unique. Code: class Solution { public boolean hasDuplicate(int[] nums) { int n = nums.length; Set set = new HashSet Time complexity:  O(n) Space complexity: O(n)

Spring-Boot externalize logback configuration file (logback-spring.xml)

Many times there is a need to externalize the logging configuration file in spring boot, so that without making any code level/ jar level changes we can easily change the logging configuration. For example If we deliver any spring boot application as a jar we dont want the logging file name to be hardcoded and to be present on any specific location, so If we have an opportunity to change the logging configuration file we can easily set the logging file location or name. Or take another example where we want to change the logging level or add logger for a new package we can easily do it if logging configuration file is externalized. Lets see how we can externalize logback loggin configuration file - Spring boot by default uses logback for logging and If we don't provide any configuration file It uses default configuration and prints logs in console. So if we want to make changes to the logback configuration we will have to create a logbacl configuration file. Follow below steps ...

Reverse LinkedList in K nodes group - Data Structure Linked List

Image
I this problem we have to reverse the nodes of the Linkedlist k at a time and leave the remaining nodes if number of nodes is not a multiple of k. For example If we have a LinkedList having 11 nodes and we are asked to reverse it for k=3 nodes at a time, we will reverse first 3 elements then next 3 elements and then again next 3 elements and leave the last 2 elements as it is. Check below diagram to understand the example - Approach : First we will find the size of the LinkedList and divide it by k to know the number of groups which needs to be reversed. We will write a recursive method which will reverse the group of k nodes and return the last node of the reversed group. We will call this recursive function for all the groups which needs to be revesed. In above example list size is 11 and value of k is 3 so we will get total 11/3 3 groups which needs to be reversed leaving the last 2 ekements unchanged. To revese the pointed of a node we will use simple iterative approach ...

Spring Cloud - Configuration Server with Git Integration

Spring Cloud is a Spring module which provides the features to make the development of distributed cloud-based applications simple and quick. Spring cloud configuration server is also one of the features which help us in the configuration management of distributed applications. In this article, we will discuss how spring cloud configuration can be used to externalize and manage the configurations of different distributed applications for different environments from a single place. We will be using a git repository as a configuration store which is considered an ideal choice. 1. Setting up Configuration Server - We will create a simple spring boot application which with the help of some dependencies will work as our configuration server serving configuration to our client application using a git versioned configuration store. 1.1 Create a spring boot application with below-required dependencies and import it to your IDE.    <dependency> <groupId>org.springfra...

Find nth Smallest Element In Binary Search Tree

Image
In the continuation of Binary Search Tree articles here comes another exciting one. In this article we will see how we can find the nth smallest element in a Binary Search Tree. Let's take the example of binary search tree given in the figure below (Fig 1.0) and see how to find its 3rd smallest element. Fig 1.0 Explanation - One thing we know for sure that we will have to traverse through this tree if we want to find any element, if so then what could be better than In-Order Traversal in this case because it gives us the nodes in ascending order. Now once we know how to traverse in ascending order only thing left is to take a counter to check whether we have traversed the n (3 in this example) nodes. Once the counter reach the value n we know its our nth smallest element. We will use the similar approach in the implementation give below, we will traverse the tree using In-Order traversal and take a counter to check the number of nodes we have traversed and as soon as the counter...