📜  去除重复的第一个字符后,最后剩下的字符和二进制字符串的字符翻转

📅  最后修改于: 2021-10-26 05:36:06             🧑  作者: Mango

鉴于长度为N的二进制字符串str,任务是通过重复删除字符串的第一个字符和翻转字符串的所有字符,如果删除的字符是“0”,找到最后一个字符从字符串中删除。

例子:

原始的方法:要解决这个问题,最简单的方法是遍历字符串的字符。对于遇到的每个字符,删除字符串的第一个字符并检查删除的字符为“0” 。如果发现为真,则翻转字符串的所有字符。最后,打印上次迭代中删除的字符。请按照以下步骤解决问题:

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

高效方法:上述方法可以基于以下观察进行优化:

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

  • 如果 N 等于 1,则答案是str[0]本身,否则。
  • 检查str[N – 2] (For N>=2) 是否为“1” 。如果发现为真,则打印str[N – 1]。
  • 否则,打印(‘1’ – str[N – 1] + ‘0’)

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the last removed
// character from the string
char lastRemovedCharacter(string str)
{
    // Stores length of the string
    int n = str.length();
 
    // Base Case:
    if (n == 1)
        return str[0];
 
    // If the second last
    // character is '0'
    if (str[n - 2] == '0') {
 
        return ('1' - str[n - 1] + '0');
    }
 
    // If the second last
    // character is '1'
    else
        return str[n - 1];
}
 
// Driver Code
int main()
{
    string str = "10010";
    cout << lastRemovedCharacter(str);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the last removed
// character from the String
static char lastRemovedCharacter(char []str)
{
    // Stores length of the String
    int n = str.length;
 
    // Base Case:
    if (n == 1)
        return str[0];
 
    // If the second last
    // character is '0'
    if (str[n - 2] == '0') {
 
        return (char)('1' - str[n - 1] + '0');
    }
 
    // If the second last
    // character is '1'
    else
        return str[n - 1];
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "10010";
    System.out.print(lastRemovedCharacter(str.toCharArray()));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
  
# Function to find the last removed
# character from the string
def lastRemovedCharacter(str):
     
    # Stores length of the string
    n = len(str)
  
    # Base Case:
    if (n == 1):
        return ord(str[0])
  
    # If the second last
    # character is '0'
    if (str[n - 2] == '0'):
        return (ord('1') -
                ord(str[n - 1]) +
                ord('0'))
  
    # If the second last
    # character is '1'
    else:
        return ord(str[n - 1])
  
# Driver Code
if __name__ == '__main__':
     
    str = "10010"
     
    print(chr(lastRemovedCharacter(str)))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
    
class GFG{
    
// Function to find the last removed
// character from the String
static char lastRemovedCharacter(char []str)
{
     
    // Stores length of the String
    int n = str.Length;
  
    // Base Case:
    if (n == 1)
        return str[0];
  
    // If the second last
    // character is '0'
    if (str[n - 2] == '0')
    {
        return (char)('1' - str[n - 1] + '0');
    }
  
    // If the second last
    // character is '1'
    else
        return str[n - 1];
}
    
// Driver Code
public static void Main()
{
    string str = "10010";
     
    Console.Write(lastRemovedCharacter(
        str.ToCharArray()));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
0

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程