Character Formation Check

Determine if the second string STR2 can be constructed using characters from the first string STR1. Both strings may include any characters.

Input:

The first line contains an integer T, the number of test cases.
For each test case:
The first line contains the string STR1.
The second line contains the string STR2.

Output:

For each test case, return "YES" if STR2 can be formed from the characters of STR1, otherwise return "NO".

Example:

Input:
STR1 = "codingninjas", STR2 = "sing"
Output:
"YES"
Explanation:

The second string, "sing", can be formed using characters "s", "i", "n", "g" from the first string "codingninjas".

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ |STR1|, |STR2| ≤ 1000
  • Time Limit: 1 sec

Note:

There's no need to print the output. Implement the function that returns the expected results.
AnswerBot
2d

Check if second string can be formed using characters from the first string.

  • Iterate through each character in STR2 and check if it exists in STR1.

  • Use a hashmap to store the frequency of characters in ...read more

Help your peers!
Add answer anonymously...
Nagarro Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter