给定一个数字字符串S,它最初仅由三种类型的字符0、1和2组成,并且执行以下两个操作:
- 出现两个连续的1可以用3代替。
- 出现两个连续的2可以用4代替。
给定的任务是找到可以通过使用操作形成的不同字符串的总数。
例子:
Input: S = “0110000022”
Output: 4
Explanation:
There can be four different formed by using the operations, the four strings are
“0110000022”, “030000022”, “03000004”, “011000004”
Input: S = “111”
Output: 3
方法:
为了解决这个问题,我们使用了动态编程方法。我们定义一个数组dp来存储长度等于其各自索引的可能字符串的计数。
- 初始化dp [0] = dp [1] = 1。
- 对于介于[1,N-1]之间的任何索引i,如果该索引处的字符为’1’或’2’并且等于其先前索引的字符,则添加可能由长度i-1组成的字符串和i-2 。否则,它与长度i-1相同。
dp[i + 1] = dp[i] + dp[i-1] if S[i] == S[i-1] where S[i] is either ‘1’ or ‘2’.
dp[i + 1] = dp[i] otherwise.
- 返回dp [n]作为可能的不同字符串的计数。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function that prints the
// number of different strings
// that can be formed
void differentStrings(string s)
{
// Computing the length of
// the given string
int n = s.length();
vector dp(n + 1);
// Base case
dp[0] = dp[1] = 1;
// Traverse the given string
for (int i = 1; i < n; i++) {
// If two consecutive 1's
// or 2's are present
if (s[i] == s[i - 1]
&& (s[i] == '1'
|| s[i] == '2'))
dp[i + 1]
= dp[i] + dp[i - 1];
// Otherwise take
// the previous value
else
dp[i + 1] = dp[i];
}
cout << dp[n] << "\n";
}
// Driver Code
int main()
{
string S = "0111022110";
differentStrings(S);
return 0;
}
Java
// Java implementation of the above approach
import java.io.*;
class GFG{
// Function that prints the
// number of different strings
// that can be formed
static void differentStrings(String s)
{
// Computing the length of
// the given string
int n = s.length();
int[] dp = new int[n + 1];
// Base case
dp[0] = dp[1] = 1;
// Traverse the given string
for(int i = 1; i < n; i++)
{
// If two consecutive 1's
// or 2's are present
if (s.charAt(i) == s.charAt(i - 1) &&
(s.charAt(i) == '1' ||
s.charAt(i) == '2'))
dp[i + 1] = dp[i] + dp[i - 1];
// Otherwise take the
// previous value
else
dp[i + 1] = dp[i];
}
System.out.println(dp[n]);
}
// Driver code
public static void main(String[] args)
{
String S = "0111022110";
differentStrings(S);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation of
# the above approach
# Function that prints the
# number of different strings
# that can be formed
def differentStrings(s):
# Computing the length of
# the given string
n = len(s)
dp = [0] * (n + 1)
# Base case
dp[0] = dp[1] = 1
# Traverse the given string
for i in range (1, n):
# If two consecutive 1's
# or 2's are present
if (s[i] == s[i - 1] and
(s[i] == '1' or
s[i] == '2')):
dp[i + 1] = dp[i] + dp[i - 1]
# Otherwise take
# the previous value
else:
dp[i + 1] = dp[i]
print (dp[n])
# Driver Code
if __name__ == "__main__":
S = "0111022110"
differentStrings(S)
# This code is contributed by Chitranayal
C#
// C# implementation of the above approach
using System;
class GFG{
// Function that prints the
// number of different strings
// that can be formed
static void differentStrings(string s)
{
// Computing the length of
// the given string
int n = s.Length;
int[] dp = new int[n + 1];
// Base case
dp[0] = dp[1] = 1;
// Traverse the given string
for(int i = 1; i < n; i++)
{
// If two consecutive 1's
// or 2's are present
if (s[i] == s[i - 1] &&
(s[i] == '1' ||
s[i] == '2'))
dp[i + 1] = dp[i] + dp[i - 1];
// Otherwise take the
// previous value
else
dp[i + 1] = dp[i];
}
Console.Write(dp[n]);
}
// Driver code
public static void Main()
{
string S = "0111022110";
differentStrings(S);
}
}
// This code is contributed by Code_Mech
输出:
12
时间复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。