热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查在所有可能的“?”替换中,给定的二进制字符串中是否出现子字符串“10”带1或0

检查在所有可能的“?”替换中,给定的二进制字符串中是否出现子字符串“10”带 1 或 0原文:https://www . gee

检查在所有可能的“?”替换中,给定的二进制字符串中是否出现子字符串“10”带 1 或 0

原文:https://www . geeksforgeeks . org/check-if-substring-10-in-给定二进制-string-in-all-replacements-with-1 或-0/

给定一个仅由“0”“1”“?”组成的弦 S* ,任务是检查在字符*“?”的每一个可能的替换中是否存在子串“10”*带 *10

示例:

输入:S =“1?0"
输出:
解释:
以下是“?”的所有可能替代项:


  1. 替换“?”用 0 将字符串修改为“100”。在修改字符串中,出现子字符串“10”。

  2. 替换“?”用 1 将字符串修改为“110”。在修改字符串中,出现子字符串“10”。

从以上所有可能的替换中,子字符串“10”出现在所有替换中,因此,打印“是”。

输入: S=??"
T3】输出:否

方法:给定的问题可以通过使用 贪婪方法 来解决,该方法基于以下观察:如果字符串 S 包含许多连续的“?”,可以换成单个'?'最坏的情况下我们可以全部用 1s 或者 0s 代替。

因此,想法是通过替换连续的“?”从给定的字符串 S 创建一个新的字符串带单“?”然后检查是否存在“10”“1?0" 作为子串,那么在所有可能的替换后就有可能得到 "10" 作为子串,因此,打印。否则,打印

下面是上述方法的实现:

C++


// C++ Program to implement
// the above approach
#include <iostream>
using namespace std;
// Function to check it possible to get
// "10" as substring after all possible
// replacements
string check(string S, int n)
{
  // Initialize empty string ans
  string ans = "";
  int c = 0;
  // Run loop n times
  for (int i = 0; i < n; i++) {
    // If char is "?", then increment
    // c by 1
    if (S[i] == '?') {
      c++;
    }
    else {
      // Continuous '?' characters
      if (c) {
        ans += "?";
      }
      c = 0;
      ans += S[i];
    }
  }
  // Their is no consecutive "?"
  if (c) {
    ans += "?";
  }
  // Check if "10" or "1?0" exists
  // in the string ans or not
  if (ans.find("10") != -1 || ans.find("1?0") != -1) {
    return "Yes";
  }
  else {
    return "No";
  }
}
// Driver code
int main()
{
  string S = "1?0";
  int n = S.size();
  string ans = check(S, n);
  cout << ans;
  return 0;
}
// This code is contributed by parthmanchanda81


Java 语言(一种计算机语言,尤用于创建网站)


// Java program for the above approach
import java.io.*;
class GFG {
 // Returns true if s1 is substring of s2
    static int isSubstring(String s1, String s2)
    {
        int M = s1.length();
        int N = s2.length();
        /* A loop to slide pat[] one by one */
        for (int i = 0; i <= N - M; i++) {
            int j;
            /* For current index i, check for
            pattern match */
            for (j = 0; j < M; j++)
                if (s2.charAt(i + j) != s1.charAt(j))
                    break;
            if (j == M)
                return i;
        }
        return -1;
    }
// Function to check it possible to get
// "10" as substring after all possible
// replacements
static String check(String S, int n)
{
  // Initialize empty string ans
  String ans = "";
  int c = 0;
  // Run loop n times
  for (int i = 0; i < n; i++) {
    // If char is "?", then increment
    // c by 1
    if (S.charAt(i) == '?') {
      c++;
    }
    else {
      // Continuous '?' characters
      if (c != 0) {
        ans += "?";
      }
      c = 0;
      ans += S.charAt(i);
    }
  }
  // Their is no consecutive "?"
  if (c != 0) {
    ans += "?";
  }
  // Check if "10" or "1?0" exists
  // in the string ans or not
  if (isSubstring("10", S) != -1 || isSubstring("1?0", S) != -1) {
    return "Yes";
  }
  else {
    return "No";
  }
}
// Driver Code
public static void main (String[] args)
{
    String S = "1?0";
        int n = S.length();
        String ans = check(S, n);
        System.out.println(ans);
}
}
// This code is contributed by avijitmondal1998.


Python 3


# Python program for the above approach
# Function to check it possible to get
# "10" as substring after all possible
# replacements
def check(S, n):
    # Initialize empty string ans
    ans = ""
    c = 0
    # Run loop n times
    for _ in range(n):
        # If char is "?", then increment
        # c by 1
        if S[_] == "?":
            c += 1
        else:
            # Continuous '?' characters
            if c:
                ans += "?"
            # Their is no consecutive "?"
            c = 0
            ans += S[_]
    # "?" still left
    if c:
        ans += "?"
    # Check if "10" or "1?0" exists
    # in the string ans or not
    if "10" in ans or "1?0" in ans:
        return "Yes"
    else:
        return "No"
# Driver Code
if __name__ == '__main__':
    S = "1?0"
    ans = check(S, len(S))
    print(ans)


C


// C# program for the approach
using System;
using System.Collections.Generic;
class GFG {
 // Returns true if s1 is substring of s2
    static int isSubstring(string s1, string s2)
    {
        int M = s1.Length;
        int N = s2.Length;
        /* A loop to slide pat[] one by one */
        for (int i = 0; i <= N - M; i++) {
            int j;
            /* For current index i, check for
            pattern match */
            for (j = 0; j < M; j++)
                if (s2[i + j] != s1[j])
                    break;
            if (j == M)
                return i;
        }
        return -1;
    }
// Function to check it possible to get
// "10" as substring after all possible
// replacements
static string check(string S, int n)
{
  // Initialize empty string ans
  string ans = "";
  int c = 0;
  // Run loop n times
  for (int i = 0; i < n; i++) {
    // If char is "?", then increment
    // c by 1
    if (S[i] == '?') {
      c++;
    }
    else {
      // Continuous '?' characters
      if (c != 0) {
        ans += "?";
      }
      c = 0;
      ans += S[i];
    }
  }
  // Their is no consecutive "?"
  if (c != 0) {
    ans += "?";
  }
  // Check if "10" or "1?0" exists
  // in the string ans or not
  if (isSubstring("10", S) != -1 || isSubstring("1?0", S) != -1) {
    return "Yes";
  }
  else {
    return "No";
  }
}
    // Driver Code
    public static void Main()
    {
        string S = "1?0";
        int n = S.Length;
        string ans = check(S, n);
        Console.Write(ans);
    }
}
// This code is contributed by sanjoy_62.


java 描述语言


  <script>
        // Javascript Program to implement
        // the above approach
        // Function to check it possible to get
        // "10" as substring after all possible
        // replacements
        function check(S, n) {
            // Initialize empty string ans
            ans = ""
            c = 0
            // Run loop n times
            for (let i = 0; i < n; i++) {
                // If char is "?", then increment
                // c by 1
                if (S[i] == "?")
                    c += 1
                else
                    // Continuous '?' characters
                    if (c != 0)
                        ans += "?"
                // Their is no consecutive "?"
                c = 0
                ans += S[i]
                // "?" still left
                if (c != 0)
                    ans += "?"
            }
            // Check if "10" or "1?0" exists
            // in the string ans or not
            if (ans.includes('10') || ans.includes('1?0'))
                return "Yes"
            else
                return "No"
        }
        // Driver Code
        S = "1?0"
        ans = check(S, S.length)
        document.write(ans)
// This code is contributed by Potta Lokesh
    script>

Output: 

Yes

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

注意:同样的方法也可以用于子串“00”/“01”/“11”以及微小的改变。


推荐阅读
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 在给定的数组中,除了一个数字外,其他所有数字都是相同的。任务是找到这个唯一的不同数字。例如,findUniq([1, 1, 1, 2, 1, 1]) 返回 2,findUniq([0, 0, 0.55, 0, 0]) 返回 0.55。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
author-avatar
飞轶尘埃_130
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有