Category: Java programming question
-
LeetCode 901: Online Stock Span
Problem Statement Design a class StockSpanner that collects daily stock prices and returns the span of the stock’s price for the current day. The span of the stock’s price today is defined as the maximum number of consecutive days (including today) the price of the stock was less than or equal to today’s price. Implement…
-
LeetCode 567: Permutation in String
Problem Statement Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. A permutation of a string is a rearrangement of its letters. For example, “ab” has permutations “ab” and “ba”. Example 1:Input: s1 = “ab”, s2 = “eidbaooo”Output: trueExplanation: “ba” is a permutation of “ab” and…
-
LeetCode 438: Find All Anagrams in a String
Problem Statement Given two strings s and p, return all start indices of p’s anagrams in s. You may return the answer in any order. An anagram is a rearrangement of letters. For example, “abc” and “cab” are anagrams. Example 1:Input: s = “cbaebabacd”, p = “abc”Output: [0, 6]Explanation: Example 2:Input: s = “abab”, p…
-
LeetCode 1447: Simplified Fractions
Problem Statement Given an integer n, return all simplified fractions between 0 and 1 (exclusive) such that the denominator is less than or equal to n. A simplified fraction is a fraction where the numerator and denominator are coprime (i.e., their greatest common divisor is 1). Example 1:Input: n = 2Output: [“1/2”] Example 2:Input: n…
-
LeetCode 328: Odd Even Linked List
Problem Statement Given the head of a singly linked list, group all odd-indexed nodes together followed by even-indexed nodes. Example 1: Example 2: Constraints: Approach to Solve the Problem (Simple Language) Idea: Steps: Why it works: Java Code Solution Dry Run Example Input: Step-by-step Execution: Final List: Textual Diagram for Understanding Complexity Analysis This two-pointer…
-
LeetCode 918: Maximum Sum Circular Subarray
Problem Statement You are given a circular integer array nums of length n. Return the maximum possible sum of a non-empty subarray of nums. Example 1: Example 2: Approach to Solve the Problem (Simple Language) Idea: Steps: Why it works: Java Code Solution Dry Run Example Input: Step-by-step Execution: Result: 10 Textual Diagram for Understanding…
-
LeetCode 208: Implement Trie
Problem Statement A Trie (Prefix Tree) is a tree data structure used to efficiently store and search strings. Implement a Trie with the following operations: Example: Approach to Solve the Problem (Simple Language) Idea: Operations: Why Trie works efficiently: Java Code Solution Dry Run Example Input: Step-by-step Execution: Textual Diagram for Understanding Complexity Analysis
-
LeetCode 402: Remove K Digits
Problem Statement You are given a string num representing a non-negative integer and an integer k. Example 1: Example 2: Approach to Solve the Problem (Stack-based Greedy) Idea: Steps: Why this works (Greedy): Java Code Solution Dry Run Example Input: Step-by-step Execution: Textual Diagram for Understanding Complexity Analysis
-
LeetCode 540: Single Element in a Sorted Array
Problem Statement You are given a sorted array nums where every element appears exactly twice, except for one element that appears only once. Return the single element that appears only once. Constraints: Example 1: Example 2: Approach to Solve the Problem (Binary Search) Idea: Steps: Why it works: Java Code Solution (Binary Search) Dry Run…
-
LeetCode 733: Flood Fill
Problem Statement You are given a 2D integer array image representing an image, and three integers sr, sc, and newColor. Return the modified image. Example: Constraints: Approach to Solve the Problem (Simple Language) Idea: DFS Steps (Recursive): Java Code Solution (DFS) Dry Run Example Input: Step-by-step Execution: Final Image: Textual Diagram for Understanding Complexity Analysis
