Ninja and substrings

Ninja has been given a string 'STR' containing only lowercase alphabetic characters. Ninja has to find the number of all the different possible substrings of size two that appear in 'STR' as contiguous substrings.

For example:

If the string is “abcd”, then all possible substrings of size two are { “ab”, “bc”, “cd”}.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The next ‘T’ lines contain a string 'STR' which denotes the input string.
Output Format:
For each test case, return all different substrings of size two that appear in 'STR' as contiguous substrings.

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 5*10^3
2 <= |STR| <= 10^3

Time limit: 1 sec
AnswerBot
1y

The task is to find all the different possible substrings of size two that appear in a given string as contiguous substrings.

  • Iterate through the string and extract substrings of size two

  • Store the subs...read more

CodingNinjas
author
2y
Brute Force

The basic idea is to iterate through the ‘STR’ and store all the substrings of size 2 in a HashSet and finally return the size of this HashSet.

The steps are as follows:

  1. Declare a HashSet ...read more
Help your peers!
Add answer anonymously...
Gainsight Associate Software Engineer 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
Get AmbitionBox app

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