📌  相关文章
📜  给定数字字符串中最小非素数子序列的长度

📅  最后修改于: 2022-05-13 01:56:07.192000             🧑  作者: Mango

给定数字字符串中最小非素数子序列的长度

给定一个由数字[1, 9]组成的大小为N的字符串S ,任务是找到字符串中最小子序列的长度,使其不是素数。

例子:

方法:解决这个问题的想法是基于以下观察:如果从字符串S中删除j或多于j个字符,则字符串是素数,那么答案应该大于j 。基于这一事实,形成所有字符串,使得从该字符串中删除元素给出一个素数。上述直觉中所有可能的字符串都是{2, 3, 5, 7, 23, 37, 53, 73}因此子序列的最大可能大小是3 。因此,想法是遍历大小为1和大小为2的所有子序列,如果发现子序列包含至少 1 个不存在于列表中的元素,则大小可能是12 。否则,大小将为3 。请按照以下步骤解决问题:

  • 将布尔变量标志初始化为假。
  • 初始化一个空字符串dummy。
  • 使用变量j迭代范围[0, N)并执行以下任务:
    • 如果第j 个位置的字符不等于2357,则打印答案为1,flag的值设置为true并中断。
  • 如果flagtrue ,则返回,否则执行以下任务。
  • 使用变量j迭代范围[0, N)并执行以下任务:
    • 使用变量j1迭代范围[j+1, N)并执行以下任务:
      • jj1位置的字符构建一个虚拟字符串。
      • 如果虚拟字符串不等于2、3、5、7、23、37、5373 ,则将答案打印为2 ,并将flag的值设置为true并中断。
  • 如果标志false并且字符串S的长度大于等于3 ,则打印3作为答案,否则打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the smallest
// length of resultant subsequence
void findMinimumSubsequence(
    string S)
{
    bool flag = false;
    string dummy;
 
    // Check for a subsequence of size 1
    for (int j = 0; j < S.length(); j++)
    {
        if (S[j] != '2' && S[j] != '3' && S[j] != '5' && S[j] != '7')
        {
            cout << 1;
            flag = true;
            break;
        }
    }
 
    // Check for a subsequence of size 2
 
    if (!flag)
    {
 
        for (int j = 0;
             j < S.length() - 1; j++)
        {
 
            for (int j1 = j + 1;
                 j1 < S.length(); j1++)
            {
 
                dummy = S[j] + S[j1];
 
                if (dummy != "23" && dummy != "37" && dummy != "53" && dummy != "73")
                {
                    cout << 2;
                }
                if (flag = true)
                    break;
            }
            if (flag = true)
                break;
        }
    }
 
    // If none of the above check is
    // successful then subsequence
    // must be of size 3
  
    if (!flag)
    {
        if (S.length() >= 3)
        {
 
            // Never executed
            cout << 3;
        }
        else
        {
            cout << -1;
        }
    }
}
 
// Driver Code
int main()
{
    string S = "237";
    findMinimumSubsequence(S);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the smallest
    // length of resultant subsequence
    public static void findMinimumSubsequence(
        String S)
    {
        boolean flag = false;
        StringBuilder dummy = new StringBuilder();
 
        // Check for a subsequence of size 1
        for (int j = 0; j < S.length(); j++) {
            if (S.charAt(j) != '2' && S.charAt(j) != '3'
                && S.charAt(j) != '5'
                && S.charAt(j) != '7') {
                System.out.println(1);
                flag = true;
                break;
            }
        }
 
        // Check for a subsequence of size 2
        if (!flag) {
        loop:
            for (int j = 0;
                 j < S.length() - 1; j++) {
 
                for (int j1 = j + 1;
                     j1 < S.length(); j1++) {
 
                    dummy = new StringBuilder(
                        Character.toString(S.charAt(j)));
                    dummy.append(S.charAt(j1));
 
                    if (!dummy.toString().equals("23")
                        && !dummy.toString().equals("37")
                        && !dummy.toString().equals("53")
                        && !dummy.toString().equals("73")) {
                        System.out.println(2);
                        flag = true;
                        break loop;
                    }
                }
            }
        }
 
        // If none of the above check is
        // successful then subsequence
        // must be of size 3
        if (!flag) {
            if (S.length() >= 3) {
 
                // Never executed
                System.out.println(3);
            }
            else {
                System.out.println(-1);
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "237";
        findMinimumSubsequence(S);
    }
}


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to find the smallest
    // length of resultant subsequence
    static void findMinimumSubsequence(string S)
    {
        bool flag = false;
        string dummy = "";
 
        // Check for a subsequence of size 1
        for (int j = 0; j < S.Length; j++) {
            if (S[j] != '2' && S[j] != '3' && S[j] != '5'
                && S[j] != '7') {
                Console.WriteLine(1);
                flag = true;
                break;
            }
        }
 
        // Check for a subsequence of size 2
        if (!flag) {
 
            for (int j = 0; j < S.Length - 1; j++) {
 
                for (int j1 = j + 1; j1 < S.Length; j1++) {
 
                    dummy = S[j].ToString()
                            + S[j1].ToString();
 
                    if (dummy != "23" && dummy != "37"
                        && dummy != "53" && dummy != "73") {
                        Console.WriteLine(2);
                    }
                    if (flag == true)
                        break;
                    else
                        flag = true;
                }
                if (flag == true)
                    break;
            }
        }
 
        // If none of the above check is
        // successful then subsequence
        // must be of size 3
 
        if (flag == false) {
            if (S.Length >= 3) {
 
                // Never executed
                Console.WriteLine(3);
            }
            else {
                Console.WriteLine(-1);
            }
        }
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "237";
        findMinimumSubsequence(S);
    }
}
 
// This code is contributed by ukasp.


Python3
# Python 3 program for the above approach
 
# Function to find the smallest
# length of resultant subsequence
def findMinimumSubsequence(S):
    flag = False
    dummy = ''
 
    # Check for a subsequence of size 1
    for j in range(len(S)):
        if (S[j] != '2' and S[j] != '3' and S[j] != '5' and S[j] != '7'):
            print(1)
            flag = True
            break
 
    # Check for a subsequence of size 2
    if (flag == False):
        for j in range(len(S)):
            for j1 in range(j + 1,len(S),1):
                dummy = S[j] + S[j1]
 
                if (dummy != "23" and dummy != "37" and dummy != "53" and dummy != "73"):
                    print(2)
                if (flag == True):
                    break
                else:
                    flag = True
                     
            if (flag == True):
                break
 
    # If none of the above check is
    # successful then subsequence
    # must be of size 3
    if (flag == False):
        if (len(S) >= 3):
            # Never executed
            print(3)
        else:
            print(-1)
     
# Driver Code
if __name__ == '__main__':
    S = "237"
    findMinimumSubsequence(S)
     
    # This code is contributed by ipg2016107.


Javascript


输出:
2

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