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
4mo

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!
Select
Add answer anonymously...

Top Technical Trainee Interview Questions Asked at Nagarro

Q. Detect and Remove Loop in Linked List For a given singly linked list, identify i...read more
Q. Trapping Rainwater Problem Statement You are given an array ARR of long type, wh...read more
Q. Spiral Order Traversal of a Binary Tree Given a binary tree with N nodes, your t...read more
Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits