Cover image for Amazon
Logo

Amazon

Engaged Employer

Amazon

Add an Interview

Interview Question

Quality Assurance Engineer Interview

-

Amazon

Find the union of two strings?

Interview Answers

14 Answers

1

public static void UnionOfStrings() { String s1 = "XYZ"; String s2 = "YZOP"; String s3 = s1.concat(s2); char ch[] = s3.toCharArray(); Set setChars = new TreeSet(); for(char c : ch) { setChars.add(c); } System.out.println(setChars.toString()); } Output : [O, P, X, Y, Z]

Deepika on

0

public static void IntersectionOfStrings() { String s1 = "XYZYYP"; String s2 = "YZOPP"; Set setChars = new TreeSet(); char c[] = s1.toCharArray(); for(char x : c) { if(s2.indexOf(x)!=-1) { setChars.add(x); } } System.out.println(setChars.toString()); } OUTPUT : [P, Y, Z]

Deepika on

0

public static void IntersectionOfStrings() { String s1 = "yybbb78"; String s2 = "y7"; if(s1.length() > s2.length()) intersect(s1,s2); else intersect(s2,s1); } public static void intersect(String first, String second) { Set setChars = new TreeSet(); char c[] = first.toCharArray(); for(char x : c) { if(second.indexOf(x)!=-1) { setChars.add(x); } } System.out.println(setChars.toString()); }

Deepika on

0

# python 2.7 a = 'once up on time' b = 'time is money' outcome = [] for i in a: for j in b: if i == j: outcome.append(i) print 'Union of two strings: ',outcome

Ashish on

0

a = 'once up on time' b = 'time is money' A = a.split(" ") B = b.split( " ") C = A + B for i in A: for j in B: if i == j: C.remove(i) print (C) D = ' '.join(C) print (D)

Ashritha on

0

a = 'once up on time' b = 'time is money' A = a.split(" ") B = b.split( " ") C = A + B C = set(C) print (C) D = ' '.join(C) print (D)

Ashritha on

0

# Prompt: Find the union of two strings in Python 3 # Inputs first_string = 'once upon a time' second_string = 'time is money' # Functional Code outcome = [] # Set "outcome" to empty list def union_filter(a, b): for a in first_string: for b in second_string: if a == b: outcome.append(a) return outcome # Driver Code output = union_filter(first_string, second_string) print(f"Union of two strings: {output}")

Interview Dude on

0

C# for Union AND intersection: Uses LINQ Library using System; using System.Linq; namespace Union_of_two_strings { class Program { /// /// Find the union and Intersection of two strings /// - assume its for all characters not words. /// - UNION: only common characters (case sensitive) should be in the results including white space if any. /// - INTERSECTION: only unique characters (case sensitive) should be in the results including white space if any. /// static void Main() { // create two test strings with NO Commonality string one = "abcd"; string two = "XYZ"; UnionizeTwoStrings(one, two); IntersectTwoStrings(one, two); // create two test strings low & upper different Case one = "xyz"; two = "XYZ"; UnionizeTwoStrings(one, two); IntersectTwoStrings(one, two); // create two test strings with some Commonality 'Brown' one = "How Now Brown"; two = "Brown Cow"; UnionizeTwoStrings(one, two); IntersectTwoStrings(one, two); // create two test strings with empty sets one = ""; two = ""; UnionizeTwoStrings(one, two); IntersectTwoStrings(one, two); // create one test string with null one = ""; two = "One Two Three"; UnionizeTwoStrings(one, two); IntersectTwoStrings(one, two); // Pause the closing of the console window Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); } /// /// Process the test strings from Main - Union combines the common characters (case sensitive) /// /// private static void UnionizeTwoStrings(string one, string two) { // Need to convert strings into char array char[] oneChar = one.ToCharArray(); char[] twoChar = two.ToCharArray(); // use LINQ union function to combine the CHAR[] var unionResults = oneChar.Union(twoChar); // output results Console.WriteLine("UNION:\nString 1:\t{0}\nString 2:\t{1}", one, two); Console.Write("Results:\t{"); // iterate out the results foreach (var item in unionResults) { Console.Write("{0}", item); } Console.Write("}\n\n"); } /// /// Process the test strings from Main - Intersect combines the unique characters (case sensitive) /// /// private static void IntersectTwoStrings(string one, string two) { // Need to convert strings into char array char[] oneChar = one.ToCharArray(); char[] twoChar = two.ToCharArray(); // use LINQ union function to combine the CHAR[] var intersectResults = oneChar.Intersect(twoChar); // output results Console.WriteLine("INTERSECT:\nString 1:\t{0}\nString 2:\t{1}", one, two); Console.Write("Results:\t{"); // iterate out the results foreach (var item in intersectResults) { Console.Write("{0}", item); } Console.Write("}\n\n"); } } }

Andrew G on

0

a='ABCA' b='CBWQ' op=set(a+b) print(op) >>> op {'W', 'Q', 'B', 'A', 'C'}

Using python on

0

private static String unionOfTwoStrings(String firstString, String secondString) { char[] changeStringToChar = firstString.concat(secondString).toCharArray(); Set setChars = new TreeSet(); for (char c : changeStringToChar) { setChars.add(c); } return setChars.toString(); }

KD on

0

var str1 = "usha"; var str2 = "asha"; char[] strAry1 = str1.ToCharArray(); char[] strAry2 = str2.ToCharArray(); while (strAry1.Length>0) { int freq = 0; var strFirst = strAry1.First(); for (int i = 0; i 0) { Console.Write(strFirst); } strAry1 =strAry1.Where(val=>val!=strFirst).ToArray(); freq = 0; }

C# Solution on

0

using System; public class Program { public static void Main() { string input = "zxcvdf"; string input2 = "asxcdlklkdf"; UnionOfString(input,input2); } public static void UnionOfString(string firstString,string secondString) { string output = ""; foreach(char val in firstString) { foreach(char val2 in secondString) { if(val==val2 && !output.Contains(val.ToString())) { output = output + val.ToString(); } } } Console.WriteLine(output); } }

commonsense on

0

for i in range(len(a)): c=[] c.append(a[i]) for j in range(len(b)): if b[j]==c[0]: union.append(b[j])

Aravind Satyanarayanan on

0

public class Union2Strings { //union of 2 strings public static void main(String[] args) { // TODO Auto-generated method stub String a = "usha"; String b = "asha"; String c = a.concat(b); System.out.println(c); char[] ch = c.toCharArray(); Set set = new TreeSet(); Set set1 = new TreeSet(); for(char s : ch) { if(!set.add(s)) { set1.add(s); } else { set1.add(s); } } System.out.println(set1);

Usha on

Add Answers or Comments

To comment on this, Sign In or Sign Up.