Category: Java programming question
-
Problem 67: Add Binary
You are given two binary strings a and b.You must return their sum, also as a binary string. Binary numbers use digits 0 and 1.Addition rules are similar to normal addition, but with base 2. Understanding the Problem Example: a = “11”b = “1”Output = “100” Explanation:11 (binary) = 31 (binary) = 13 + 1…
-
Problem 210: Course Schedule II
You are given the number of courses and a list of prerequisite pairs.Your task is to find an ordering in which you can finish all courses. This is a classic directed graph problem where each course is a node, and each prerequisite is a directed edge. If a valid ordering exists, return it.If it is…
-
Problem 395: Longest Substring with At Least K Repeating Characters
Understanding the Problem You are given a string s and an integer k.You must find the length of the longest substring where every character appears at least k times. For example: Input: s = “aaabb”, k = 3Output: 3Valid substring: “aaa” (each character frequency ≥ 3) Another example: Input: s = “ababbc”, k = 2Output:…
-
LeetCode Problem 50: Pow(x, n)
Problem Statement Implement the function myPow(x, n) which calculates x raised to the power n (i.e., xⁿ). You must not use built-in library functions like Math.pow() and must handle both positive and negative exponents efficiently. Examples Approach: Fast Exponentiation (Binary Exponentiation) Intuition The naive approach of multiplying x by itself n times takes O(n) time…
-
Problem: Reverse Words in a String (LeetCode 151)
Problem Statement Given an input string s, reverse the order of the words.A word is defined as a sequence of non-space characters.The returned string should have exactly one space separating the words, and no leading or trailing spaces. Example 1:Input: Output: Example 2:Input: Output: Example 3:Input: Output: Approach 1: Split and Reverse This is the…
-
LeetCode Problem 1344: Angle Between Hands of a Clock
Problem Statement Given two integers hour and minutes, return the smaller angle (in degrees) between the hour hand and the minute hand of a clock. Example 1: Example 2: Example 3: Constraints: Intuition A clock has two hands — hour and minute — each rotating at different speeds. We must find the smaller angle between…
-
LeetCode Problem 100: Same Tree
Problem Statement Given the roots of two binary trees, p and q, write a function to check if they are the same tree. Two binary trees are considered the same if: Example 1: Example 2: Example 3: Intuition We need to verify two conditions simultaneously at every node: This is a classic recursive comparison problem…
-
LeetCode Problem 190: Reverse Bits
Problem Statement Reverse the bits of a given 32-bit unsigned integer. Example 1: Example 2: Constraints: Intuition The task is to reverse the binary digits of a 32-bit number. For example: If the input binary is: then the output should be: That means we need to: Step-by-Step Approach This way, bits from the right of…
-
LeetCode Problem 5: Longest Palindromic Substring
Problem Statement Given a string s, return the longest palindromic substring in s. A palindrome is a string that reads the same forward and backward. Example 1: Example 2: Constraints: Intuition A palindrome has a symmetric property: A substring is a palindrome if it reads the same when reversed. For example: We want the longest…
-
LeetCode Problem 78: Subsets
Problem Statement Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets, and the order of subsets does not matter. Example: Intuition We are asked to generate all possible combinations of elements from the array — that is, the power set. If…
