String Trimmer

String Trimmer – A Fundamental Filtering Problem

Problem Statement

You are given T test cases. Each test case consists of two strings, A and B, comprised of lowercase English letters and separated by a space.

Your task is to remove from string A all characters that are present in string B. The resulting string should preserve the original order of characters in A, excluding any characters that occur in B.

Examples

Input

2 data structures smart interviews

Output

srucures ineview

Explanation

  • In the first case, we remove all characters from "data structures" that appear in "smart", resulting in "srucures".

  • In the second case, we remove characters from "interviews" that appear in "smart", resulting in "ineview".

Best Data Structure for the Job

To determine membership of a character in B efficiently, a HashSet is ideal. It provides constant-time average lookup and allows us to quickly verify whether each character in A should be excluded

Different Approaches

Approach 1: Brute Force Character Check

Iterate through each character in A and, for each character, check if it exists in B using a nested loop.

public class StringTrimmerBruteForce { public static String filter(String A, String B) { StringBuilder result = new StringBuilder(); for (int i = 0; i < A.length(); i++) { boolean toRemove = false; for (int j = 0; j < B.length(); j++) { if (A.charAt(i) == B.charAt(j)) { toRemove = true; break; } } if (!toRemove) { result.append(A.charAt(i)); } } return result.toString(); } }

Approach 2: Frequency Array 

Use a boolean array of size 26 to mark the presence of characters in B.

public class StringTrimmerUsingArray { public static String filter(String A, String B) { boolean[] present = new boolean[26]; for (char ch : B.toCharArray()) { present[ch - 'a'] = true; } StringBuilder result = new StringBuilder(); for (char ch : A.toCharArray()) { if (!present[ch - 'a']) { result.append(ch); } } return result.toString(); } }

Approach 3: HashSet Lookup 

Use a HashSet<Character> to store characters in B, then filter A.

import java.util.*; public class StringTrimmerUsingSet { public static String filter(String A, String B) { Set<Character> excludeSet = new HashSet<>(); for (char ch : B.toCharArray()) { excludeSet.add(ch); } StringBuilder result = new StringBuilder(); for (char ch : A.toCharArray()) { if (!excludeSet.contains(ch)) { result.append(ch); } } return result.toString(); } }

⚠️Common Mistakes and Pitfalls

  • Failing to preserve the order of characters in A after filtering.

  • Assuming inputs might contain uppercase characters or spaces—if constraints don’t include them, avoid overgeneralizing.

  • Using nested loops unnecessarily, which may lead to timeouts on larger inputs.

📊 Time and Space Complexity Comparison

Approach        Time Complexity        Space Complexity
Brute Force        O(n * m)        O(1)
Frequency Array        O(n + m)        O(1)
HashSet Lookup        O(n + m)        O(m)

🌍Real-world Applications

  • Building email or comment filters that block blacklisted words or characters.

  • Designing input sanitizers for user-generated content.

  • Lightweight text preprocessing in NLP tasks where stopword or keyword removal is necessary.


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries