From 0db4839b3bc4e75a4277e2d460ad673737b640ea Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 18 Feb 2025 23:17:02 +0530 Subject: [PATCH] Create 2375. Construct Smallest Number From DI String --- .... Construct Smallest Number From DI String | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2375. Construct Smallest Number From DI String diff --git a/2375. Construct Smallest Number From DI String b/2375. Construct Smallest Number From DI String new file mode 100644 index 0000000..8f7dd0b --- /dev/null +++ b/2375. Construct Smallest Number From DI String @@ -0,0 +1,25 @@ +class Solution { +public: + bool isMatched(string s, string pattern) { + int n = pattern.size(); + for (int i = 0; i < n; i++) { + if (pattern[i] == 'I' && s[i] > s[i + 1] || + pattern[i] == 'D' && s[i] < s[i + 1]) { + return false; + } + } + return true; + } + string smallestNumber(string pattern) { + int n = pattern.size(); + string ans = ""; + for (int i = 1; i <= n + 1; i++) { + ans.push_back(i + '0'); + } + + while (!isMatched(ans, pattern)) { + next_permutation(ans.begin(), ans.end()); + } + return ans; + } +};