Truncate Sentence A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation). For example, “Hello World”, “HELLO”, and “hello world hello world” are all sentences. You are given a sentence s…
Category: Algorithms and Datastructure
leetcode 1854 solution in java
Maximum Population Year Solution This above problem is taken from https://leetcode.com/problems/maximum-population-year/ Solution:- Approach:- Code:- public static int methodeName(int[][] arr) { //defined one array years with size 2051 int[] years = new int[2051]; //Now hover over the goven 2d array for (int[] log : arr) { //given array is {1952,1961},{1953,1961 } //so for first iteration log…
leetcode 1859 solution in java
Sorting the Sentence A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters. A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence. For example,…
LeetCode 1869 Solution in Java
Longer Contiguous Segments of Ones than Zeros Solution:- Approach:- Code:- public boolean checkZeroOnes(String s) { int countOne = 0; int countZero = 0; int maxOne=0; int maxZero = 0; for ( char m:s.toCharArray()) { if (m==’0′) { countZero ++; countOne=0; } if (m==’1′) { countOne ++; countZero=0; } maxOne = Math.max(maxOne,countOne); maxZero = Math.max(maxZero,countZero); }…
Leet code 35 Java Solution: Search insert position
Problem:- Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Solution:- Summary of the problem:- Here we have to search in the given sorted array that if the given number…
Leetcode 27 Java Solution: Remove Element
Problem Statement:- Given an array nums and a value val, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn’t matter what you leave beyond…
Leet Code 20 java: Valid Parentheses Algorithm
Given an expression string exp, write a program to examine whether the pairs and the orders of “{“, “}”, “(“, “)”, “[“, “]” are correct in exp. Example: Logic: Given String:- Step 1:- Take this String as an input of the function. This Function will return the Boolean. Step 2: Check all the opening brackets…
Merge Two Sorted Lists : Leet Code Solution
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Ans:- To solve this approach, we will create a Dummy Head. And as the two given list will be sorted, so we have to only compare the individual…
Big O Notation
I want to explain it in a few simple words. Basically this is a tool to express how efficient your algorithm is. One point we need to remember that, the efficiency of an algorithm is very much relative, because it depends on many other factors like number of programs or process is currently running, the…
Queue Data Structure for Beginners
Queue is a kind of data structure where the order of the element matters. It’s like a line in-front of movie ticket line. The person who came last in the queue will stand at the rear position of the line. And the person who joined the line at the very first would be the person…