Expedia Group Interview Question: Remove all characters of a st... | Glassdoor.co.in

Interview Question

Software Development Engineer In Test (SDET) II Interview Gurgaon, Haryana

Remove all characters of a string from another string. The

  algorithm should be less than O(m*n) where m & n are lengths of strings and the space used should be less than O(m+n).
Tags:
algorithm
Answer

Interview Answer

3 Answers

0

I have written a program using Hashmap and StringBuilder.

Interview Candidate on 05-Jun-2013
1

An efficient solution to the problem can done using simple pattern matching. A sample solution goes as follows :

public class SampleMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String charStream = "ABABCBABABABABABATGBHacbfoabcbahasacbJKOPLMNBGDESRABADACAJALAMNBADDHEMSZPQ";
        String noise = "ABCPQR";
        System.out.println("CharStream : " + charStream + "\n" +
                "Noise : " + noise + "\n" +
                "FliteredCharStream : " + func(charStream, noise));
    }

    private static String func(String charStream, String noise) {
        String regex = "[" + noise.toLowerCase() + noise.toUpperCase() + "]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(charStream);
        return m.replaceAll("");
    }

}

Venkata Soma Sekhar Sahukara on 19-Jun-2013
0

import java.util.Arrays;

public class Solution{
    public static void main(String[] args){
        System.out.println(remove("as","asasfsg"));
    }

    public static String remove(String s1, String s2){
        boolean[] flags = new boolean[255];
        char[] a1 = s1.toCharArray();
        char[] a2 = s2.toCharArray();
        String str = "";

        Arrays.fill(flags,false);

        for(int i=0; i=a1.length && i

solution in Java using boolean array on 13-Jun-2018

Add Answers or Comments

To comment on this, Sign In or Sign Up.