Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

942. DI String Match

Easy79.4% Acceptance
ArrayTwo PointersString
Asked by:
M
Microsoft
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:

  • s[i] == 'I' if perm[i] < perm[i + 1], and
  • s[i] == 'D' if perm[i] > perm[i + 1].

Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.

Example 1:

Input: s = "IDID"
Output: [0,4,1,3,2]

Example 2:

Input: s = "III"
Output: [0,1,2,3]

Example 3:

Input: s = "DDI"
Output: [3,2,0,1]

Constraints:

  • 1 <= s.length <= 105
  • s[i] is either 'I' or 'D'.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key idea in #942 DI String Match is to construct a permutation of numbers from 0 to n that follows the pattern described by a string of 'I' (increase) and 'D' (decrease). A common and efficient strategy is a greedy two-pointer approach. Instead of trying every permutation, maintain two boundaries representing the smallest and largest unused numbers.

When the current character in the string is 'I', place the smallest available number and move the lower pointer forward. When the character is 'D', place the largest available number and move the upper pointer backward. This greedy choice guarantees the relative order required by the pattern while ensuring all numbers remain unique.

The process continues for each character in the string, and one number remains for the final position. Because each element is processed once, the algorithm runs efficiently with O(n) time complexity and uses O(n) space for the result array.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy Two-Pointer ConstructionO(n)O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (12)

Two-Pointer Technique

Description: Use two pointers, or indices - one starting at 0 and the other at 'n'. For each 'I' in the string, assign the lowest available number and increment the 'low' index. For each 'D', assign the highest available number and decrement the 'high' index. This approach efficiently fills the permutation array by maximizing the immediate decision at each step.

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), excluding the output array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int* diStringMatch(char * s, int* returnSize){
5    int

Explanation

This C solution uses a two-pointer approach. It initializes 'low' and 'high' to track the currently available smallest and largest numbers. As it iterates through the string 's', it assigns numbers based on whether 'I' or 'D' is encountered, thereby constructing the permutation.

Greedy Character Matching

Description: The Greedy Character Matching approach involves tracking two dynamic bounds, 'low' and 'high', to determine at every step what the current optimal choice for our permutation should be. By iterating over the string 's', for every 'I', append the current lowest available integer and increment it. For 'D', add the current highest integer and decrement it, ensuring the correct permutation ordering through direct engagement with the constraints.

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), discounting the result storage.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

String Matching in an Array - Leetcode 1408 - Python

NeetCodeIO
8:0110,849 views

Asked By Companies

1 companies
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersStringGreedy

Problem Stats

Acceptance Rate79.4%
DifficultyEasy
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is DI String Match asked in FAANG interviews?

Problems like DI String Match appear in interviews at large tech companies because they test greedy thinking and array manipulation. While the exact question may vary, similar pattern-based greedy problems are common in coding interviews.

What is the optimal approach for DI String Match?

The optimal approach uses a greedy two-pointer technique. Maintain the smallest and largest available numbers and assign them based on whether the current character is 'I' or 'D'. This ensures the required ordering while processing the string in a single pass.

Why does the greedy approach work for DI String Match?

The greedy strategy works because each 'I' only requires the next value to be larger and each 'D' requires it to be smaller. By always choosing the smallest or largest available number, we satisfy the condition without restricting future placements.

What data structure is best for solving DI String Match?

An array or list to store the resulting permutation is sufficient. Along with that, two integer pointers representing the current minimum and maximum available values help implement the greedy strategy efficiently.

n
=
strlen
(
s
)
;
6
int
*
result
=
(
int
*
)
malloc
(
(
n
+
1
)
*
sizeof
(
int
)
)
;
7
int
low
=
0
,
high
=
n
,
i
;
8
for
(
i
=
0
;
i
<
n
;
++
i
)
{
9
result
[
i
]
=
s
[
i
]
==
'I'
?
low
++
:
high
--
;
10
}
11
result
[
n
]
=
low
;
// or result[n] = high since low == high
12
*
returnSize
=
n
+
1
;
13
return
result
;
14
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
int
*
diStringMatch
(
char
*
s
,
int
*
returnSize
)
{
5
int
n
=
strlen
(
s
)
;
6
int
*
result
=
(
int
*
)
malloc
(
(
n
+
1
)
*
sizeof
(
int
)
)
;
7
int
low
=
0
,
high
=
n
,
i
;
8
for
(
i
=
0
;
i
<
n
;
i
++
)
{
9
if
(
s
[
i
]
==
'I'
)
10
result
[
i
]
=
low
++
;
11
else
12
result
[
i
]
=
high
--
;
13
}
14
result
[
n
]
=
low
;
15
*
returnSize
=
n
+
1
;
16
return
result
;
17
}

Explanation

This C implementation captures the essence of greedy processing by dynamically adjusting 'low' and 'high'. It leverages pointer manipulation for assignments and appends the last remaining 'low' to complete the permutation.