Ninja and Substrings Problem Statement

Ninja has to determine all the distinct substrings of size two that can be formed from a given string 'STR' comprising only lowercase alphabetic characters. These substrings must appear as contiguous sequences in 'STR'.

Example:

Input:
"abcd"
Output:
["ab", "bc", "cd"]

Input:

The first line contains a single integer 'T' representing the number of test cases. The next 'T' lines each contain a string 'STR'.

Output:

For each test case, output all unique contiguous substrings of size two from 'STR'. Each test case result should be presented on a new line.

Constraints:

  • 1 <= T <= 5*103
  • 2 <= |STR| <= 103
  • Time limit: 1 sec
Note:
You are not required to print anything; it has been handled for you internally.
AnswerBot
4mo

Find all unique contiguous substrings of size two from a given string.

  • Iterate through the string and extract substrings of size two

  • Use a set to store unique substrings

  • Return the set as an array of str...read more

Help your peers!
Select
Add answer anonymously...

MagicBricks Software Developer interview questions & answers

A Software Developer was asked Q. Launch modes on android
A Software Developer was asked Q. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Software Developer was asked Q. Ninja and Substrings Problem Statement Ninja has to determine all the distinct s...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Launch modes on android
A Software Developer was asked Q2. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Software Developer was asked Q3. Ninja and Substrings Problem Statement Ninja has to determine all the distinct s...read more
MagicBricks Software Developer 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