Interview Question

Software Development Engineer In Test (SDET) Interview

Given a string, find whether it has any permutation of

  another string. Need to be efficient
Tags:
technical
Answer

Interview Answer

4 Answers

0

isnt it enough if we find out whether the characters in the second string is present in the first?
(also the number of times each character appears).?

Anonymous on 27-Feb-2013
0

The characters need to be consecutive. For example, if the second string is "abc", we need to find out whether the first string has one of the following: "abc" , "acb" , "bac" , "bca" , "cab" or "cba"

Anonymous on 27-Feb-2013

This post has been removed.
Please see our Community Guidelines or Terms of Service for more information.

1

public boolean test(String main, String sub){
        char[] a = main.toCharArray();
        char[] b = sub.toCharArray();
        char[] c = sub.toCharArray();
        Arrays.sort(b);

        for(int i = 0; i<a.length-b.length+1; i++){
            int k=i;
            for(int j=0; j<b.length; j++){
                c[j] = a[k++];
            }
            Arrays.sort(c);
            if(Arrays.equals(b,c))
                return true;
        }
        return false;
    }

Rachana Koneru on 07-Mar-2013

Add Answers or Comments

To comment on this Question, Sign In with Facebook or Sign Up