可以删除的最大位数,以便剩余整数是辅音
给定一个表示N位整数的字符串S ,任务是找到可以删除的最大位数,以便从辅音整数中删除剩余的位数。
请注意,0 和 1 也被视为非质数。
例子:
Input: S = “237”
Output: 1
Explanation: In the given integer, S[1] can be removed. Hence, S = “27”, which is the smallest possible integer that is non-prime. Therefore, the maximum number of digit that can be removed is 1.
Input: S = “35”
Output: -1
Explanation: It is not possible to create a non-prime integer using the above steps.
方法:给定的问题可以通过以下观察来解决:所有具有3位或更多位的字符串都包含长度最多为2位的数字子序列,使得该子序列表示非素数整数。使用此观察,可以使用以下步骤解决给定问题:
- 创建一个数组prime[] ,它存储给定整数是否为[0, 100)范围内的所有整数的素数。可以使用 Eratosthenes 筛高效地创建该阵列。
- 迭代给定的字符串str[]并检查是否存在任何非素数的1位字符串,即{0, 1, 4, 6, 8, 9} 。
- 如果不存在单个数字字符串,请检查给定字符串的长度是否 <= 2 。在这种情况下,如果S表示的整数是素数,则返回-1 ,否则返回0 。
- 否则,返回N – 2 ,这将是所需的答案。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Stores if integer representing
// the index is prime of not
bool prime[100];
// Function to calculate prime[]
// using sieve of eratosthenes
void sieve()
{
// Set all integers as prime
for (int i = 0; i < 100; i++) {
prime[i] = true;
}
// Since 0 and 1 are considered
// as the non prime integers
prime[0] = false;
prime[1] = false;
for (int i = 2; i < 100; i++) {
if (!prime[i])
continue;
for (int j = 2 * i; j < 100; j += i) {
prime[j] = false;
}
}
}
// Function to find the maximum count of
// digits that can be removed such that
// the remaining integer is non-prime
int maxCount(string S)
{
// Loop to iterate over all
// digits in string S
for (int i = 0; i < S.size(); i++) {
// If a non-prime single
// digit integer is found
if (!prime[S[i] - '0']) {
return S.length() - 1;
}
}
// If the length of string
// is at most 2
if (S.length() <= 2) {
// If S represents a
// prime integer
if (prime[stoi(S)])
return -1;
else
return 0;
}
else {
// Return Answer
return S.length() - 2;
}
}
// Driver Code
int main()
{
string S = "237";
sieve();
cout << maxCount(S);
return 0;
}
Java
// JAVA program of the above approach
import java.util.*;
class GFG
{
// Stores if integer representing
// the index is prime of not
private static boolean[] prime = new boolean[100];
// Function to calculate prime[]
// using sieve of eratosthenes
public static void sieve()
{
// Set all integers as prime
for (int i = 0; i < 100; i++) {
prime[i] = true;
}
// Since 0 and 1 are considered
// as the non prime integers
prime[0] = false;
prime[1] = false;
for (int i = 2; i < 100; i++) {
if (!prime[i])
continue;
for (int j = 2 * i; j < 100; j += i) {
prime[j] = false;
}
}
}
// Function to find the maximum count of
// digits that can be removed such that
// the remaining integer is non-prime
public static int maxCount(String S)
{
// Loop to iterate over all
// digits in string S
for (int i = 0; i < S.length(); i++)
{
// If a non-prime single
// digit integer is found
if (!prime[S.charAt(i) - '0']) {
return S.length() - 1;
}
}
// If the length of string
// is at most 2
if (S.length() <= 2) {
// If S represents a
// prime integer
if (prime[Integer.parseInt(S)])
return -1;
else
return 0;
}
else {
// Return Answer
return S.length() - 2;
}
}
// Driver Code
public static void main(String[] args)
{
String S = "237";
sieve();
System.out.print(maxCount(S));
}
}
// This code is contributed by Taranpreet
Python3
# Stores if integer representing
# the index is prime of not
prime = []
# Function to calculate prime[]
# using sieve of eratosthenes
def sieve():
# Set all integers as prime
for i in range(100):
prime.append(True)
# Since 0 and 1 are considered
# as the non prime integers
prime[0] = False
prime[1] = False
for i in range(2, 100):
if (not prime[i]):
continue
for j in range(2*i, 100, i):
prime[j] = False
# Function to find the maximum count of
# digits that can be removed such that
# the remaining integer is non-prime
def maxCount(S):
# Loop to iterate over all
# digits in string S
N = len(S)
for i in range(N):
# If a non-prime single
# digit integer is found
if (not prime[int(S[i])]):
return N - 1
# If the length of string
# is at most 2
if (N <= 2):
# If S represents a
# prime integer
if (prime[int(S)]):
return -1
else:
return 0
else:
# Return Answer
return N - 2
# driver code
S = "237"
sieve()
print(maxCount(S))
# This code is contributed by Palak Gupta
C#
using System;
public class GFG
{
// Stores if integer representing
// the index is prime of not
private static bool[] prime = new bool[100];
// Function to calculate prime[]
// using sieve of eratosthenes
public static void sieve()
{
// Set all integers as prime
for (int i = 0; i < 100; i++) {
prime[i] = true;
}
// Since 0 and 1 are considered
// as the non prime integers
prime[0] = false;
prime[1] = false;
for (int i = 2; i < 100; i++) {
if (!prime[i])
continue;
for (int j = 2 * i; j < 100; j += i) {
prime[j] = false;
}
}
}
// Function to find the maximum count of
// digits that can be removed such that
// the remaining integer is non-prime
public static int maxCount(string S)
{
// Loop to iterate over all
// digits in string S
for (int i = 0; i < S.Length; i++)
{
// If a non-prime single
// digit integer is found
if (!prime[S[i] - '0']) {
return S.Length - 1;
}
}
// If the length of string
// is at most 2
if (S.Length <= 2) {
// If S represents a
// prime integer
if (prime[Int32.Parse(S)])
return -1;
else
return 0;
}
else {
// Return Answer
return S.Length - 2;
}
}
// Driver code
static public void Main (){
string S = "237";
sieve();
Console.Write(maxCount(S));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
1
时间复杂度: O(N)
辅助空间: O(1)