Skip to main content

Posts

Leetcode 72: Edit Distance

1. Problem Statement (Simple Explanation) You’re given two strings: word1 word2 You can transform word1 into word2 using  three  operations: Insert  a character Delete  a character Replace  a character You must return the  minimum number of operations  required. This is the classic  Levenshtein distance  problem. 2. Examples Example 1: Input: word1 = "horse" word2 = "ros" One optimal sequence: "horse" → "rorse" (replace 'h' with 'r') "rorse" → "rose" (delete 'r') "rose" → "ros" (delete 'e') Output: 3 Example 2: Input: word1 = "intention" word2 = "execution" One optimal sequence (5 operations): intention → inention (delete 't') inention → enention (replace 'i' with 'e') enention → exention (replace 'n' with 'x') exention → exection (replace 'n' with...

Equal Distinct

We need to decide if we can split array  A  into two arrays  B  and  C  such that: Every element of  A  goes to exactly one of  B  or  C . F(B) = F(C) , where  F(S)  = number of distinct elements in  S . Key Observations Let’s define: d  = number of  distinct values  in  A . multi  = number of distinct values that appear  at least twice  in  A . Important points: Every distinct value in  A  must appear in  at least one  of  B  or  C . To have  F(B) = F(C) , the total number of distinct values we can  distribute  between them must be  even , and we must be able to give each side the same count. A value that appears  at least twice  can be placed in  both   B  and  C  simultaneously by putting one occurrence in each. This  increases  both  F(B)  and  F(C)  by 1 i...