📌  相关文章
📜  通过将具有和K的相似相邻数字替换为K来最大化生成的不同字符串的数量

📅  最后修改于: 2021-04-26 18:58:23             🧑  作者: Mango

给定长度为N的数字字符串S和数字K ,任务是找到最大数目的不同字符串,其中最大的K出现在其中,如果S的任何两个相邻数字之和为K,则用K替换任意两个相邻的数字。

例子:

方法:给定的问题可以通过观察得出,对于S的某个子串,有两种类型的结果可以起作用,即它可以是xy数相等的“ xy”序列,即, “ xyxy…xyxy”或者它可以是具有一个额外xxy序列,即“ xyxy…xyxyx” ,其中x + y = K。

  • 情况1: “ xyxy…xyxy”形式的字符串可以转换为字符串“ KK…KK” ,其中出现K (子字符串的长度)/ 2次。因此,将形成的不同字符串的数量为1
  • 情况2:形式为“ xyxy…xyxyx”的字符串有一个额外的“ x”,并且可以转换为字符串“ KK…KKx” ,其中出现K (子字符串-1的长度)/ 2次。令该值为M。通过观察,可以看出在转换后的字符串x将会有(M +1)个可能的位置。

请按照以下步骤解决问题:

  • 1初始化变量ans ,用0初始化标志,用-1初始化start_index ,其中ans将存储直到索引i的答案,并且start_index将用于存储每个子串的起始索引(从该序列开始)。
  • 遍历字符串S并执行以下操作:
    • 如果任何相邻的数字S [i]S [i + 1]的总和为Kflag设置为false ,则将flag设置为true并将start_index设置i
    • 如果标志trueS [i]S [i + 1]不是K ,请将标志设置为false ,并且如果(start_index – i + 1)为奇数,则将ans乘以(start_index – i + 1 – 1)情况2中所述的/ 2 +1
    • 遍历字符串S后,如果flagtrue并且(N – start_index)为奇数,则将ans乘以(N – start_index – 1)/ 2 + 1
  • 完成上述步骤后,将ans打印为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the desired number
// of strings
void countStrings(string s, int k)
{
    // Store the count of strings
    int ans = 1;
 
    // Store the length of the string
    int len = s.size();
 
    // Initialize variable to indicate
    // the start of the substring
    int flag = 0;
 
    // Store the starting index of
    // the substring
    int start_ind;
 
    // Traverse the string
    for (int i = 0; i < len - 1; i++) {
 
        // If sum of adjacent characters
        // is  K mark the starting index
        // and set flag to 1
        if (s[i] - '0' + s[i + 1] - '0'
                == k
            && flag == 0) {
            flag = 1;
            start_ind = i;
        }
 
        // If sum of adjacent characters
        // is not K and the flag variable
        // is set, end the substring here
        if (flag == 1
            && s[i] - '0'
                       + s[i + 1] - '0'
                   != k) {
 
            // Set flag to 0 denoting
            // the end of substring
            flag = 0;
 
            // Check if the length of the
            // substring formed is odd
            if ((i - start_ind + 1) % 2 != 0)
 
                // Update the answer
                ans *= (i - start_ind + 1 - 1)
                           / 2
                       + 1;
        }
    }
 
    // If flag is set and end of string
    // is reached, mark the end of substring
    if (flag == 1
        && (len - start_ind) % 2 != 0)
 
        // Update the answer
        ans *= (len - start_ind) / 2 + 1;
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    string S = "313";
    int K = 4;
 
    // Function Call
    countStrings(S, K);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
       
// Function to find the desired number
// of strings
static void countStrings(String s, int k)
{
     
    // Store the count of strings
    int ans = 1;
     
    // Store the length of the string
    int len = s.length();
     
    // Initialize variable to indicate
    // the start of the substring
    int flag = 0;
     
    // Store the starting index of
    // the substring
    int start_ind = 0;
  
    // Traverse the string
    for(int i = 0; i < len - 1; i++)
    {
         
        // If sum of adjacent characters
        // is  K mark the starting index
        // and set flag to 1
        if (s.charAt(i) - '0' +
            s.charAt(i + 1) - '0' == k && flag == 0)
        {
            flag = 1;
            start_ind = i;
        }
         
        // If sum of adjacent characters
        // is not K and the flag variable
        // is set, end the substring here
        if (flag == 1 && s.charAt(i) - '0' +
            s.charAt(i + 1) - '0' != k)
        {
             
            // Set flag to 0 denoting
            // the end of substring
            flag = 0;
             
            // Check if the length of the
            // substring formed is odd
            if ((i - start_ind + 1) % 2 != 0)
             
                // Update the answer
                ans *= (i - start_ind + 1 - 1) / 2 + 1;
        }
    }
  
    // If flag is set and end of string
    // is reached, mark the end of substring
    if (flag == 1 && (len - start_ind) % 2 != 0)
     
        // Update the answer
        ans *= (len - start_ind) / 2 + 1;
  
    // Print the answer
    System.out.println(ans);
}
  
// Driver Code
public static void main(String[] args)
{
    String S = "313";
    int K = 4;
     
    // Function Call
    countStrings(S, K);
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python program to implement
# the above approach
 
# Function to find the desired number
# of strings
def countStrings(s, k) :
     
    # Store the count of strings
    ans = 1
  
    # Store the length of the string
    lenn = len(s)
  
    # Initialize variable to indicate
    # the start of the substring
    flag = 0
  
    # Traverse the string
    for i in range(lenn - 1):
  
        # If sum of adjacent characters
        # is  K mark the starting index
        # and set flag to 1
        if (ord(s[i]) - ord('0') + ord(s[i + 1]) - ord('0')
                == k
            and flag == 0) :
            flag = 1
            start_ind = i
         
        # If sum of adjacent characters
        # is not K and the flag variable
        # is set, end the substring here
        if (flag == 1
            and ord(s[i]) - ord('0')
                       + ord(s[i + 1]) - ord('0')
                   != k) :
  
            # Set flag to 0 denoting
            # the end of substring
            flag = 0
  
            # Check if the length of the
            # substring formed is odd
            if ((i - start_ind + 1) % 2 != 0) :
  
                # Update the answer
                ans *= (i - start_ind + 1 - 1) // 2 + 1
          
    # If flag is set and end of string
    # is reached, mark the end of substring
    if (flag == 1
        and (lenn - start_ind) % 2 != 0):
  
        # Update the answer
        ans *= (lenn - start_ind) // 2 + 1
  
    # Prthe answer
    print(ans)
 
# Driver Code
S = "313"
K = 4
  
# Function Call
countStrings(S, K)
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program for the above approach
using System;
 
class GFG{
       
// Function to find the desired number
// of strings
static void countStrings(String s, int k)
{
     
    // Store the count of strings
    int ans = 1;
     
    // Store the length of the string
    int len = s.Length;
     
    // Initialize variable to indicate
    // the start of the substring
    int flag = 0;
     
    // Store the starting index of
    // the substring
    int start_ind = 0;
  
    // Traverse the string
    for(int i = 0; i < len - 1; i++)
    {
         
        // If sum of adjacent characters
        // is  K mark the starting index
        // and set flag to 1
        if (s[i] - '0' +
            s[i + 1] - '0' == k && flag == 0)
        {
            flag = 1;
            start_ind = i;
        }
         
        // If sum of adjacent characters
        // is not K and the flag variable
        // is set, end the substring here
        if (flag == 1 && s[i] - '0' +
            s[i + 1] - '0' != k)
        {
             
            // Set flag to 0 denoting
            // the end of substring
            flag = 0;
             
            // Check if the length of the
            // substring formed is odd
            if ((i - start_ind + 1) % 2 != 0)
             
                // Update the answer
                ans *= (i - start_ind + 1 - 1) / 2 + 1;
        }
    }
  
    // If flag is set and end of string
    // is reached, mark the end of substring
    if (flag == 1 && (len - start_ind) % 2 != 0)
     
        // Update the answer
        ans *= (len - start_ind) / 2 + 1;
  
    // Print the answer
    Console.WriteLine(ans);
}
  
// Driver Code
public static void Main(String[] args)
{
    String S = "313";
    int K = 4;
     
    // Function Call
    countStrings(S, K);
}
}
 
// This code is contributed by Princi Singh


输出:
2

时间复杂度: O(N)
辅助空间: O(N)