Skip to main content

Posts

Leetcode 88: Merge Sorted Array

  1. Problem Statement (Simple Explanation) You’re given: nums1: an integer array of length m + n The  first m elements  are valid, sorted in non-decreasing order. The  last n elements  are placeholders (zeros) to accommodate merging. nums2: an integer array of length n, sorted in non-decreasing order. Integers m and n: number of valid elements in nums1 and nums2. You must: Merge  nums2 into nums1, so that the first m + n elements of nums1 become a sorted array. Do this  in-place  (modify nums1, don’t return a new array). Time complexity O(m + n) is required. 2. Examples Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6],       n = 3 Arrays to merge: [1,2,3] and [2,5,6] Result: [1,2,2,3,5,6] Output (nums1 after): [1,2,2,3,5,6] Example 2: Input: nums1 = [1], m = 1 nums2 = [],  n = 0 Nothing to merge;...

Leetcode 87: Scramble String

1. Problem Statement (Simple Explanation) We define a  scramble  operation on a string s: If |s| == 1: stop. If |s| > 1: Split s at some index i into two non-empty substrings: x = s[0..i-1], y = s[i..end]. Randomly choose to keep them in order (x + y) or swap them (y + x). Recursively apply the same process to x and y. Given two strings s1 and s2  of equal length , return true if s2 is a  scrambled string  of s1 under this definition, otherwise false. Constraints: 1 <= length(s1) <= 30 s1.length == s2.length Both consist of lowercase letters. 2. Examples Example 1: Input: s1 = "great", s2 = "rgeat" One valid scrambling sequence: "great" → split "gr" + "eat" No swap: "gr" + "eat" Recurse: "gr" → "g" + "r" → swap → "rg" Recurse: "eat" → "e" + "at" → "e" + "a" + "t" (no swaps) F...