📜  字母和数字字符数相等的最长子数组

📅  最后修改于: 2021-05-04 14:49:21             🧑  作者: Mango

给定一个字母数字字符数组。任务是找到最长的连续子数组,该数组具有相等数量的字母(字母)和数字(数字)。打印此子数组的开始和结束索引。如果有多个结果,则输出起始索引最低的一个。

例子:

方法:我们必须考虑一个事实,即所有数字都可以被相同地对待(这意味着0和5可以被视为相同,但是0和’a’不能被视为相同),并且所有字母都可以以相似的方式被相同地对待。办法。因此,我们遍历数组,并用’0’替换每个字母,并用’1’替换每个数字。
然后,此问题减少为https://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/。
在修改了上述算法的代码以解决此问题后,我们提出了以下代码来解决该问题。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the starting and the
// ending index of the sub-array with equal
// number of alphabets and numeric digits
void findSubArray(int arr[], int n)
{
    int sum = 0;
    int maxsize = -1, startindex;
    for (int i = 0; i < n; i++) {
  
        // If its an alphabet
        if (isalpha(arr[i])) {
            arr[i] = 0;
        }
  
        // Else its a number
        else {
            arr[i] = 1;
        }
    }
  
    // Pick a starting point as i
    for (int i = 0; i < n - 1; i++) {
        sum = (arr[i] == 0) ? -1 : 1;
  
        // Consider all sub-arrays starting from i
        for (int j = i + 1; j < n; j++) {
            (arr[j] == 0) ? (sum += -1) : (sum += 1);
  
            // If this is a 0 sum sub-array then
            // compare it with maximum size sub-array
            // calculated so far
            if (sum == 0 && maxsize < j - i + 1) {
                maxsize = j - i + 1;
                startindex = i;
            }
        }
    }
  
    // If no valid sub-array found
    if (maxsize == -1)
        cout << maxsize;
    else
        cout << startindex << " " << (startindex + maxsize - 1);
}
  
// Driver code
int main()
{
    int arr[] = { 'A', 'B', 'X', 4, 6, 'X', 'a' };
    int size = sizeof(arr) / sizeof(arr[0]);
  
    findSubArray(arr, size);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
      
    static boolean isalpha(int input_char)
    {
        if ((input_char >= 65 && input_char <= 90) 
            || (input_char >= 97 && input_char <= 122)) 
            return true;
              
        return false;
    }
      
    // Function to find the starting and the 
    // ending index of the sub-array with equal 
    // number of alphabets and numeric digits 
    static void findSubArray(int arr[], int n) 
    { 
        int sum = 0; 
        int maxsize = -1, startindex = 0; 
        for (int i = 0; i < n; i++) 
        { 
      
            // If its an alphabet 
            if (isalpha(arr[i]))
            { 
                arr[i] = 0; 
            } 
      
            // Else its a number 
            else 
            { 
                arr[i] = 1; 
            } 
        } 
      
        // Pick a starting point as i 
        for (int i = 0; i < n - 1; i++) 
        { 
            sum = (arr[i] == 0) ? -1 : 1; 
      
            // Consider all sub-arrays starting from i 
            for (int j = i + 1; j < n; j++)
            { 
                if(arr[j] == 0) 
                    sum += -1; 
                else
                    sum += 1; 
      
                // If this is a 0 sum sub-array then 
                // compare it with maximum size sub-array 
                // calculated so far 
                if (sum == 0 && maxsize < j - i + 1) 
                { 
                    maxsize = j - i + 1; 
                    startindex = i; 
                } 
            } 
        } 
      
        // If no valid sub-array found 
        if (maxsize == -1) 
            System.out.println(maxsize); 
        else
            System.out.println(startindex + " " + (startindex + maxsize - 1)); 
    } 
      
    // Driver code 
    public static void main (String[] args)
    {
          
        int arr[] = { 'A', 'B', 'X', 4, 6, 'X', 'a' }; 
        int size = arr.length; 
      
        findSubArray(arr, size); 
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to find the starting and the
# ending index of the sub-array with equal
# number of alphabets and numeric digits
def findSubArray(arr, n):
    sum = 0
    maxsize = -1
    startindex=0
    for i in range(n):
  
        # If its an alphabet
        if (arr[i].isalpha()):
            arr[i] = 0
          
  
        # Else its a number
        else :
            arr[i] = 1
          
    # Pick a starting poas i
    for i in range(n-1):
        if arr[i]=='1':
            sum=1
        else:
            sum=-1    
  
        # Consider all sub-arrays starting from i
        for j in range(i+1,n):
            if arr[j]==0:
                sum-=1
            else:
                sum+=1    
  
            # If this is a 0 sum sub-array then
            # compare it with maximum size sub-array
            # calculated so far
            if (sum == 0 and maxsize < j - i + 1) :
                maxsize = j - i + 1
                startindex = i
              
          
    # If no valid sub-array found
    if (maxsize == -1):
        print(maxsize,end=" ")
    else:
        print(startindex,(startindex + maxsize - 1))
  
  
# Driver code
arr=['A', 'B', 'X', '4', '6', 'X', 'a'] 
size =len(arr)
  
findSubArray(arr, size)
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach 
using System;
class GFG 
{
      
    static bool isalpha(int input_char)
    {
        if ((input_char >= 65 && input_char <= 90) 
            || (input_char >= 97 && input_char <= 122)) 
            return true;
              
        return false;
    }
      
    // Function to find the starting and the 
    // ending index of the sub-array with equal 
    // number of alphabets and numeric digits 
    static void findSubArray(int []arr, int n) 
    { 
        int sum = 0; 
        int maxsize = -1, startindex = 0; 
        for (int i = 0; i < n; i++) 
        { 
      
            // If its an alphabet 
            if (isalpha(arr[i]))
            { 
                arr[i] = 0; 
            } 
      
            // Else its a number 
            else
            { 
                arr[i] = 1; 
            } 
        } 
      
        // Pick a starting point as i 
        for (int i = 0; i < n - 1; i++) 
        { 
            sum = (arr[i] == 0) ? -1 : 1; 
      
            // Consider all sub-arrays starting from i 
            for (int j = i + 1; j < n; j++)
            { 
                if(arr[j] == 0) 
                    sum += -1; 
                else
                    sum += 1; 
      
                // If this is a 0 sum sub-array then 
                // compare it with maximum size sub-array 
                // calculated so far 
                if (sum == 0 && maxsize < j - i + 1) 
                { 
                    maxsize = j - i + 1; 
                    startindex = i; 
                } 
            } 
        } 
      
        // If no valid sub-array found 
        if (maxsize == -1) 
            Console.WriteLine(maxsize); 
        else
        Console.WriteLine(startindex + " " + (startindex + maxsize - 1)); 
    } 
      
    // Driver code 
    public static void Main()
    {
          
        int []arr = { 'A', 'B', 'X', 4, 6, 'X', 'a' }; 
        int size = arr.Length; 
      
        findSubArray(arr, size); 
    }
}
  
// This code is contributed by anuj_67..


输出:
1 4