Given two strings s1 and s2, remove every character from s1 that is present in s2. Return the resulting string.
Note: Both strings contain only lowercase English letters, and |s1| > |s2|.
Examples:
Input: s1 = "computer", s2 = "cat"
Output: "ompuer"
Explanation: After removing characters(c, a, t) from string1 we get "ompuer".Input: s1 = "occurrence", s2 = "car"
Output: "ouene"
Explanation: After removing characters (c, a, r) from string1 we get "ouene".
Table of Content
[Naive Approach] Using In-built Search function - O(|s1| × |s2|) Time and O(1) Space
For every character of s1, the idea is to check whether the same character exists in s2 using a built-in search function. If it is found, skip it; otherwise, add it to the result.
- Initialize an empty result string.
- Traverse each character of s1.
- Use the built-in search function to check whether the character exists in s2.
- If the character is not found, add it to the result.
- Return the result.
#include <bits/stdc++.h>
using namespace std;
// Returns s1 after removing all characters
// that are present in s2.
string removeChars(string &s1, string &s2)
{
string res;
// Traverse every character of s1.
for (char ch : s1)
{
// Check whether the current character
// is present in s2.
if (s2.find(ch) == string::npos)
{
// If not present, keep the character.
res += ch;
}
}
return res;
}
int main()
{
string s1 = "occurrence";
string s2 = "car";
cout << removeChars(s1, s2) << endl;
return 0;
}
import java.util.*;
class GFG {
// Returns s1 after removing all characters
// that are present in s2.
static String removeChars(String s1, String s2)
{
StringBuilder res = new StringBuilder();
// Traverse every character of s1.
for (char ch : s1.toCharArray()) {
// Check whether the current character
// is present in s2.
if (s2.indexOf(ch) == -1) {
// If not present, keep the character.
res.append(ch);
}
}
return res.toString();
}
public static void main(String[] args)
{
String s1 = "occurrence";
String s2 = "car";
System.out.println(removeChars(s1, s2));
}
}
# Returns s1 after removing all characters
# that are present in s2.
def removeChars(s1, s2):
res = ""
# Traverse every character of s1.
for ch in s1:
# Check whether the current character
# is present in s2.
if ch not in s2:
# If not present, keep the character.
res += ch
return res
# Driver Code
if __name__ == "__main__":
s1 = "occurrence"
s2 = "car"
print(removeChars(s1, s2))
using System;
class GFG {
// Returns s1 after removing all characters
// that are present in s2.
static string removeChars(string s1, string s2)
{
string res = "";
// Traverse every character of s1.
foreach(char ch in s1)
{
// Check whether the current character
// is present in s2.
if (s2.IndexOf(ch) == -1) {
// If not present, keep the character.
res += ch;
}
}
return res;
}
static void Main()
{
string s1 = "occurrence";
string s2 = "car";
Console.WriteLine(removeChars(s1, s2));
}
}
// Returns s1 after removing all characters
// that are present in s2.
function removeChars(s1, s2)
{
let res = "";
// Traverse every character of s1.
for (let ch of s1) {
// Check whether the current character
// is present in s2.
if (s2.indexOf(ch) === -1) {
// If not present, keep the character.
res += ch;
}
}
return res;
}
// Driver Code
let s1 = "occurrence";
let s2 = "car";
console.log(removeChars(s1, s2));
Output
ouene
[Expected Approach] Using Frequency Array - O(|s1| + |s2|) Time and O(1) Space
The idea is to store the frequency of every character of s2 in a frequency array of size 26. Then traverse s1 and keep only those characters whose frequency in s2 is 0.
- Create a frequency array freq[26] initialized with 0.
- Traverse s2 and increment the frequency of each character.
- Traverse s1.
- If freq[ch - 'a'] == 0, add the character to the result.
- Return the resulting string.
#include <bits/stdc++.h>
using namespace std;
// Returns s1 after removing all characters
// that are present in s2.
string removeChars(string &s1, string &s2)
{
// Stores the frequency of each character in s2.
int freq[26] = {0};
// Traverse every character of s2.
for (char ch : s2)
{
// Increment the frequency of the current character.
freq[ch - 'a']++;
}
string res;
// Traverse every character of s1.
for (char ch : s1)
{
// Keep the character only if it is not present in s2.
if (freq[ch - 'a'] == 0)
{
res += ch;
}
}
return res;
}
int main()
{
string s1 = "occurrence";
string s2 = "car";
cout << removeChars(s1, s2) << endl;
return 0;
}
import java.util.*;
class GFG {
// Returns s1 after removing all characters
// that are present in s2.
static String removeChars(String s1, String s2)
{
// Stores the frequency of each character in s2.
int[] freq = new int[26];
// Traverse every character of s2.
for (char ch : s2.toCharArray()) {
// Increment the frequency of the current
// character.
freq[ch - 'a']++;
}
StringBuilder res = new StringBuilder();
// Traverse every character of s1.
for (char ch : s1.toCharArray()) {
// Keep the character only if it is not present
// in s2.
if (freq[ch - 'a'] == 0) {
res.append(ch);
}
}
return res.toString();
}
public static void main(String[] args)
{
String s1 = "occurrence";
String s2 = "car";
System.out.println(removeChars(s1, s2));
}
}
# Returns s1 after removing all characters
# that are present in s2.
def removeChars(s1, s2):
# Stores the frequency of each character in s2.
freq = [0] * 26
# Traverse every character of s2.
for ch in s2:
# Increment the frequency of the current character.
freq[ord(ch) - ord('a')] += 1
res = ""
# Traverse every character of s1.
for ch in s1:
# Keep the character only if it is not present in s2.
if freq[ord(ch) - ord('a')] == 0:
res += ch
return res
# Driver Code
if __name__ == "__main__":
s1 = "occurrence"
s2 = "car"
print(removeChars(s1, s2))
using System;
class GFG {
// Returns s1 after removing all characters
// that are present in s2.
static string removeChars(string s1, string s2)
{
// Stores the frequency of each character in s2.
int[] freq = new int[26];
// Traverse every character of s2.
foreach(char ch in s2)
{
// Increment the frequency of the current
// character.
freq[ch - 'a']++;
}
string res = "";
// Traverse every character of s1.
foreach(char ch in s1)
{
// Keep the character only if it is not present
// in s2.
if (freq[ch - 'a'] == 0) {
res += ch;
}
}
return res;
}
static void Main()
{
string s1 = "occurrence";
string s2 = "car";
Console.WriteLine(removeChars(s1, s2));
}
}
// Returns s1 after removing all characters
// that are present in s2.
function removeChars(s1, s2)
{
// Stores the frequency of each character in s2.
let freq = new Array(26).fill(0);
// Traverse every character of s2.
for (let ch of s2) {
// Increment the frequency of the current character.
freq[ch.charCodeAt(0) - "a".charCodeAt(0)]++;
}
let res = "";
// Traverse every character of s1.
for (let ch of s1) {
// Keep the character only if it is not present in
// s2.
if (freq[ch.charCodeAt(0) - "a".charCodeAt(0)]
=== 0) {
res += ch;
}
}
return res;
}
// Driver Code
let s1 = "occurrence";
let s2 = "car";
console.log(removeChars(s1, s2));
Output
ouene