Learnitweb

Category: Java programming question

  • Number of Islands

    Problem Description You’re given a 2D grid of characters ‘1’ (land) and ‘0’ (water). You need to find the number of islands in the grid. Rules: Example Input: Output: Explanation: Approach Overview The problem is fundamentally a graph traversal problem: We can solve this using: We’ll use DFS in this tutorial because it’s intuitive and…

  • Valid Parenthesis String

    Problem Statement: Given a string s containing only ‘(‘, ‘)’, and ‘*’, return true if the string is valid. A string is valid if: Examples: Example 1: Example 2: Example 3: Example 4: The challenge is that ‘*’ can be anything: This means we need to consider multiple possible interpretations. Greedy Two-Pointer Method (Optimized O(n)…

  • Product of Array Except Self

    Problem Statement: Given an integer array nums, return an array answer such that: Example: Input: Output: Explanation: Intuition and Approach (Without Division) We cannot use division, so we need to calculate the product of elements before and after each index, then multiply them. Idea: Let: But to save space, we reuse the result array and…

  • Contiguous Array

    Problem Statement You are given a binary array nums (only contains 0s and 1s).Your task is to find the maximum length of a contiguous subarray with an equal number of 0 and 1. Example Input: Output: Explanation: Approach: Using HashMap and Prefix Sum Core Idea: Why it works: Java Code Explanation Step-by-Step Dry Run nums…

  • Last Stone Weight

    Problem Statement You are given an array of integers stones where each element represents the weight of a stone. Every turn, you take the two heaviest stones and smash them together: This process continues until only one stone is left or no stones are left. Return the weight of the last remaining stone, or 0…

  • Diameter of Binary Tree

    Problem Statement Given the root of a binary tree, you need to compute the diameter of the tree. Diameter is defined as the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Approach Java Implementation Explanation Example Walkthrough Consider this tree:…

  • Backspace String Compare

    Problem Statement You are given two strings s and t. Each string may contain the backspace character #, which means the previous character should be deleted. Your task is to determine if the two strings are equal after applying all backspace operations. Example 1: Example 2: Input: s = “a##c”, t = “#a#c”Output: trueExplanation: Example…

  • Middle of the Linked List

    Problem Statement You are given the head of a singly linked list, and your task is to return the middle node of the linked list. Example 1: Example 2: Constraints: Approach 1: Count Nodes (Two Pass) Time Complexity: O(n) – two traversals of the linked list.Space Complexity: O(1) – only a few pointers are used.…

  • Counting Elements

    Problem Breakdown Given an integer array arr, you need to determine how many elements x exist such that x + 1 is also present in arr. If there are duplicates of x, each occurrence should be counted separately. Example 1: Input: arr = [1, 2, 3] Output: 2 Explanation: Both 1 and 2 are counted…

  • Group anagrams

    Problem Statement You are given an array of strings strs. The task is to group the strings that are anagrams of each other. Definition: Two strings are anagrams if they have the same characters in the same frequency but possibly in a different order. Example: Constraints: Approach The main idea is to use a hash…