鉴于长度为N的二进制字符串str,任务是通过重复删除字符串的第一个字符和翻转字符串的所有字符,如果删除的字符是“0”,找到最后一个字符从字符串中删除。
例子:
Input: str = “1001”
Output: ‘0’
Explanation:
Removing str[0] from the string modifies str to “00”.
Removing str[0] from the string modifies str to “10”.
Removing str[0] from the string modifies str to “0”.
Since the last character removed was ‘0’, the required output is ‘0’.
Input: str = “10010”
Output: ‘0’
原始的方法:要解决这个问题,最简单的方法是遍历字符串的字符。对于遇到的每个字符,请删除字符串的第一个字符,然后检查所删除的字符为“ 0” 。如果发现是真的,则翻转字符串的所有字符。最后,打印在上一次迭代中删除的字符。请按照以下步骤解决问题:
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
Case 1:
Suppose str = “XXXXXXX0X”, where X is either ‘0’ or ‘1’.
If the character str[N – 2] is flipped, then str[N – 1] must be flipped.
Therefore, if str[N – 2] is ‘0’, then last removed character will be (‘1’ – str[N – 1] + ‘0’).
Case 2:
Suppose str = “XXXXXXX1X”, where X is either ‘0’ or ‘1.
If the character str[N – 2] is flipped, then str[N – 1] must be flipped.
Therefore, if str[N – 2] is ‘1’, then the last removed character will be str[N – 1].
请按照以下步骤解决问题:
- 检查str [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
0
时间复杂度: O(1)
辅助空间: O(1)