LeetCode 443: String Compression in Java

Day 36 of Daily DSA 🚀 Solved LeetCode 443: String Compression ✅ Problem: Given an array of characters, compress it in-place using run-length encoding. Each group of consecutive repeating characters is replaced by the character followed by its count (if count > 1). The result must be stored back in the input array. Rules: * If a group's length is 1 → append only the character * If a group's length is > 1 → append character + count * Group lengths ≥ 10 are split into multiple characters * Must use only constant extra space * Return the new length of the array Approach: Used a StringBuilder to build the compressed string by tracking consecutive character counts, then wrote the result back into the input array. Steps: 1. Append the first character to StringBuilder 2. Iterate through the array counting consecutive duplicates 3. On a new character → if count > 1, append the count 4. Append the new character and reset count 5. Handle the last group after the loop 6. Write compressed characters back into the input array ⏱ Complexity: • Time: O(n) • Space: O(n) (StringBuilder) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 12.04%) • Memory: 45.58 MB (Beats 42.35%) A great exercise in in-place array manipulation and run-length encoding — fundamentals that show up everywhere in data compression. #DSA #LeetCode #Java #Strings #RunLengthEncoding #CodingJourney #ProblemSolving

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories