Skip to content

2375 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged

2375 #207

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 2301-2400/2375_construct_smallest_number_from_di_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @param {String} pattern
# @return {String}
def smallest_number(pattern, result = [], i = -1, prev_num = 0, prev_pat = "I", used = Array.new(10, false))
return result.join if i == pattern.size

(1..9).each { |j|
if !used[j] && (prev_pat == "D" ? (j < prev_num) : (j > prev_num))
result << j
used[j] = true

found = smallest_number(pattern, result, i + 1, j, pattern[i + 1], used)
return found unless found.nil?

result.pop
used[j] = false
end
}

nil
end