[LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列

We are given?S, a length?n?string of characters from the set?{‘D‘, ‘I‘}. (These letters stand for "decreasing" and "increasing".)

A?valid permutation?is a permutation?P[0], P[1], ..., P[n]?of integers?{0, 1, ..., n}, such that for all?i:

  • If?S[i] == ‘D‘, then?P[i] > P[i+1], and;
  • If?S[i] == ‘I‘, then?P[i] < P[i+1].

How many valid permutations are there?? Since the answer may be large,?return your answer modulo?10^9 + 7.

Example 1:

Input: "DID"
Output: 5
Explanation:
The 5 valid permutations of (0, 1, 2, 3) are:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)

Note:

  1. 1 <= S.length <= 200
  2. S?consists only of characters from the set?{‘D‘, ‘I‘}.

Github 同步地址:

https://github.com/grandyang/leetcode/issues/CHANGE_ME

参考资料:

https://leetcode.com/problems/valid-permutations-for-di-sequence/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/11094525.html

时间: 2024-08-01 14:13:32

[LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列的相关文章

903. Valid Permutations for DI Sequence

We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for "decreasing" and "increasing".) A valid permutation is a permutation P[0], P[1], ..., P[n] of integers {0, 1, ..., n}, such that for all 

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4

LeetCode: Valid Parentheses 题解

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"