Sateesh Sonkamble’s Post

🚀 𝗦𝘁𝗿𝗶𝗻𝗴 𝗖𝗼𝗺𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝗼𝗹𝘃𝗲𝗱 𝗶𝗻 𝟯 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗪𝗮𝘆𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁: Given an array of characters, compress the string by replacing repeated characters with the character followed by its count. 🔗 𝗚𝗶𝘁𝗛𝘂𝗯 𝗖𝗼𝗱𝗲 👉https://lnkd.in/gap5HkW2 Example: Input: ["a","a","b","b","c","c","c"] Output: ["a","2","b","2","c","3"] 𝗜 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗲𝗱 𝟯 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀: 1️⃣ 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Create a separate result array Count repeated characters Append character and count Time Complexity: O(n) Space Complexity: O(n) 2️⃣ 𝗕𝗲𝘁𝘁𝗲𝗿 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Use two pointers Cleaner logic with left and right pointers Still uses extra space Time Complexity: O(n) Space Complexity: O(n) 3️⃣ 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Use in-place modification Maintain a write pointer Most memory efficient solution Time Complexity: O(n) Space Complexity: O(1) 𝗧𝗵𝗲 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗶𝘀 𝗯𝗲𝘀𝘁 because it avoids extra memory usage and works directly on the input array. 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴: Always try to solve a problem in multiple ways: First for correctness Then for readability Finally for optimization 𝗧𝗵𝗶𝘀 𝗶𝘀 𝗮 𝗴𝗿𝗲𝗮𝘁 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗽𝗿𝗼𝗯𝗹𝗲𝗺 because it tests: ✔️ Arrays ✔️ Two Pointers ✔️ String Manipulation ✔️ Time and Space Complexity Data Structures and Algorithms and Python are all about improving problem-solving skills step by step. #Python #DSA #CodingInterview #ProblemSolving #Algorithms #100DaysOfCode #SoftwareEngineering #Programming #LeetCode #Coding

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories