LeetCode 2788: Split Strings by Separator

💻 Daily Coding Practice – LeetCode 2788: Split Strings by Separator Today I solved a neat problem: 👉 Given an array of strings and a separator character, split each string by the separator and return all non-empty substrings in order. 🔹 Step-by-Step Approach (Manual Traversal) 1. Initialize an empty result list. 2. Loop through each string in the input list. 3. For each string: Track the start index of the current substring. Traverse each character. 4. If the character equals the separator → extract the substring from start to current index, add it if non-empty, and update start. 5. Otherwise, keep building the substring. 6. After finishing the string, add the last substring if non-empty. 7. Return the result list. This approach avoids regex overhead and gives precise control over empty substrings. ⚡ Complexity Analysis Time Complexity: Outer loop iterates over all strings → O(N) Inner loop iterates over all characters in each string → O(M) Substring operations across the string sum to O(M) Overall: O(N * M) Space Complexity: Result list stores all substrings → O(K) (where K is total substrings produced). Temporary variables use constant space. Overall: O(K) 🔑 Key Takeaways Manual traversal ensures correctness for tricky cases like consecutive separators or leading/trailing separators. Built-in split() is shorter but requires regex handling for special characters like | or $. Always filter out empty substrings to match problem requirements. 🚀 Solving problems like this daily strengthens my fundamentals and builds confidence in handling edge cases. #LeetCode #Java #ProblemSolving #DailyCodingChallenge #BackendDevelopment #LearningJourney💡

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories