📌  相关文章
📜  检查字符串数组是否可以对应特定数字 X

📅  最后修改于: 2021-09-07 03:24:53             🧑  作者: Mango

给定一个整数X和一个字符串数组str ,它代表[2, 36]范围内的任何基数中的数字,任务是通过为每个字符串分配所需的 2 到 36 基数来检查是否所有字符串都可以转换为 X,这样字符串的十进制基数等价物是 X。
例子:

方法:这个想法是通过将数组的每个数字分配给从 2 到 36 的基数,然后检查每个转换后的数字是否等于 X 或不等于 X 来将数组的每个数字转换为十进制基数。
上述方法的分步算法如下所述 –

  1. 将计数初始化为 0,以检查转换时等于 X 的数字的计数。
  2. 运行一个循环来迭代数组的数字,然后对每个数字进行迭代——
    • 运行另一个从 2 到 36 的循环以将基数分配给数字并找到该数字的十进制等价物。
    • 如果该数字的十进制等效值等于 X,则将计数增加 1 并中断循环,以便不将任何其他基数分配给相同的数字。
  3. 如果可转换为 X 的数字的数量等于数组的长度,则该数组可以对应于数字 X。

下面是上述方法的实现:

C++
// C++ implementation to check
// wheather array of strings
// can correspond to a number X
 
#include 
using namespace std;
 
// Function to find the maximum
// base possible for the number N
int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
 
// Function to find the decimal
// equivalent of the number
int toDeci(string str, int base)
{
    int len = str.size();
    int power = 1;
    int num = 0;
    int i;
    for (i = len - 1; i >= 0; i--) {
         
        // Condition to check if the
        // number is convertible
        // to another base
        if (val(str[i]) >= base) {
            return -1;
        }
        num += val(str[i]) * power;
        power = power * base;
    }
    return num;
}
 
// Function to check that the
// array can correspond to a number X
void checkCorrespond(vector str,
                                int x){
                                     
    // counter to count the numbers
    // those are convertible to X
    int counter = 0;
    int n = str.size();
 
    // Loop to iterate over the array
    for (int i = 0; i < n; i++) {
        for (int j = 2; j <= 36; j++) {
             
            // Convert the current string
            // to every base for checking
            // whether it will correspond
            // to X from any base
            if (toDeci(str[i], j) == x) {
                counter++;
                break;
            }
        }
    }
     
    // Condition to check if every
    // number of the array can
    // be converted to X
    if (counter == n)
        cout << "YES"
            << "\n";
    else
        cout << "NO"
            << "\n";
}
 
// Driver Code
int main()
{
    int x = 16;
 
    // The set of strings
    // in base from [2, 36]
    vector str =
         { "10000", "20", "16" };
    checkCorrespond(str, x);
    return 0;
}


Java
// Java implementation to check
// wheather array of Strings
// can correspond to a number X
 
class GFG{
  
// Function to find the maximum
// base possible for the number N
static int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
  
// Function to find the decimal
// equivalent of the number
static int toDeci(String str, int base)
{
    int len = str.length();
    int power = 1;
    int num = 0;
    int i;
    for (i = len - 1; i >= 0; i--) {
          
        // Condition to check if the
        // number is convertible
        // to another base
        if (val(str.charAt(i)) >= base) {
            return -1;
        }
        num += val(str.charAt(i)) * power;
        power = power * base;
    }
    return num;
}
  
// Function to check that the
// array can correspond to a number X
static void checkCorrespond(String[] str,
                                int x){
                                      
    // counter to count the numbers
    // those are convertible to X
    int counter = 0;
    int n = str.length;
  
    // Loop to iterate over the array
    for (int i = 0; i < n; i++) {
        for (int j = 2; j <= 36; j++) {
              
            // Convert the current String
            // to every base for checking
            // whether it will correspond
            // to X from any base
            if (toDeci(str[i], j) == x) {
                counter++;
                break;
            }
        }
    }
      
    // Condition to check if every
    // number of the array can
    // be converted to X
    if (counter == n)
        System.out.print("YES"
           + "\n");
    else
        System.out.print("NO"
           + "\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int x = 16;
  
    // The set of Strings
    // in base from [2, 36]
    String[] str =
         { "10000", "20", "16" };
    checkCorrespond(str, x);
}
}
 
// This code contributed by PrinciRaj1992


Python3
# Python3 implementation to check
# wheather array of strrings
# can correspond to a number X
 
# Function to find the maximum
# base possible for the number N
def val(c):
    if (c >= '0' and c <= '9'):
        return int(c)
    else:
        return c - 'A' + 10
         
# Function to find the decimal
# equivalennt of the number
def toDeci(strr, base):
     
    lenn = len(strr)
    power = 1
    num = 0
    for i in range(lenn - 1, -1, -1):
         
        # Condition to check if the
        # number is convertible
        # to another base
        if (val(strr[i]) >= base):
            return -1
         
        num += val(strr[i]) * power
        power = power * base
     
    return num
 
 
# Function to check that the
# array can correspond to a number X
def checkCorrespond(strr, x):
     
    # counter to count the numbers
    # those are convertible to X
    counter = 0
    n = len(strr)
     
    # Loop to iterate over the array
    for i in range(n):
        for j in range(2,37):
             
            # Convert the current strring
            # to every base for checking
            # whether it will correspond
            # to X from any base
            if (toDeci(strr[i], j) == x):
                counter += 1
                break
             
    # Condition to check if every
    # number of the array can
    # be converted to X
    if (counter == n):
        print("YES")
    else:
        print("NO")
 
# Driver Code
x = 16
 
# The set of strrings
# in base from [2, 36]
strr = ["10000", "20", "16"]
checkCorrespond(strr, x)
 
# This code is contributed by shubhamsingh10


C#
// C# implementation to check
// wheather array of Strings
// can correspond to a number X
using System;
 
class GFG{
   
// Function to find the maximum
// base possible for the number N
static int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
   
// Function to find the decimal
// equivalent of the number
static int toDeci(String str, int Base)
{
    int len = str.Length;
    int power = 1;
    int num = 0;
    int i;
    for (i = len - 1; i >= 0; i--) {
           
        // Condition to check if the
        // number is convertible
        // to another base
        if (val(str[i]) >= Base) {
            return -1;
        }
        num += val(str[i]) * power;
        power = power * Base;
    }
    return num;
}
   
// Function to check that the
// array can correspond to a number X
static void checkCorrespond(String[] str,
                                int x){
                                       
    // counter to count the numbers
    // those are convertible to X
    int counter = 0;
    int n = str.Length;
   
    // Loop to iterate over the array
    for (int i = 0; i < n; i++) {
        for (int j = 2; j <= 36; j++) {
               
            // Convert the current String
            // to every base for checking
            // whether it will correspond
            // to X from any base
            if (toDeci(str[i], j) == x) {
                counter++;
                break;
            }
        }
    }
       
    // Condition to check if every
    // number of the array can
    // be converted to X
    if (counter == n)
        Console.Write("YES"
           + "\n");
    else
        Console.Write("NO"
           + "\n");
}
   
// Driver Code
public static void Main(String[] args)
{
    int x = 16;
   
    // The set of Strings
    // in base from [2, 36]
    String[] str =
         { "10000", "20", "16" };
    checkCorrespond(str, x);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
YES

性能分析:

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live