Skip to content
CodeKruPro

Find the characters common to two strings

Coding problemEasy-MediumAsked in: Capgemini, Infosys

Given two strings a and b, return a string of the characters that appear in both. List each such character once, in the order it first appears in a.

Ignore spaces. Upper and lower case are different characters. If no character is shared, return an empty string.

Examples

Example 1
Input: a = "hello", b = "world"
Output: "lo"
Example 2
Input: a = "Apple", b = "apple"
Output: "ple"
A and a are different characters.
Example 3
Input: a = "abc", b = "xyz"
Output: ""

Constraints

  • The length of a and of b is between 0 and 100,000
  • Both hold printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in