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

📅  最后修改于: 2021-09-07 03:09:17             🧑  作者: Mango

给定的长度N和一个数字K A数字字符串S,任务是找到具有在它由具有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出现(子字符串的长度)/2次。让这个值为M 。通过观察,可以看出在转换后的字符串x可能有(M + 1) 个位置。

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

  • 1初始化变量ans ,用0初始化变量,用-1初始化start_index ,其中ans将把答案存储到索引i并且start_index将用于存储每个子串的起始索引,从所需的序列开始。
  • 遍历字符串S并执行以下操作:
    • 如果任何相邻数字S[i]S[i + 1]的总和为K并且flag设置为false ,则将flag设置为true并将start_index 设置i
    • 如果flagtrueS[i]S[i + 1]不是K ,则将flag设置为false并且如果(start_index – i + 1)是奇数,则将ans乘以(start_index – i + 1 – 1) /2 + 1如案例 2 中所述。
    • 遍历字符串S 后,如果flag(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
  
    # Print the 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


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live