📜  整数字符串中可被4整除的子字符串数

📅  最后修改于: 2021-05-05 02:25:53             🧑  作者: Mango

给定一个由整数0到9组成的字符串。任务是计算子字符串的数量,这些子字符串在转换为整数时可以被4整除。子字符串可能包含前导零。

例子:

Input : "124"
Output : 4
Substrings divisible by 4 are  "12", "4", "24", "124" .

Input : "04"
Output : 3
Substring divisible by 4 are "0", "4", "04" .

方法1 :(蛮力)的想法是找到给定字符串的所有子字符串,并检查子字符串是否可被4整除。
时间复杂度:O(

*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

)。

有效的解决方案:如果一个数字的最后两位可以被4整除,并且一个数字可以被4整除的数是4、8和0,那么它可以被4整除。因此,要计算可被4整除的子串的数目,我们首先计算0的个数,字符串中的4和8。然后,我们将两个连续的字符全部配对,并将其转换为整数。将其转换为整数后,我们检查其是否可被4整除。如果它可以被4整除,那么所有以最后两个字符结尾的此类子串都可以被4整除。现在,此类子串的数量基本上是pair的第一个字符的索引。为了更加清楚,考虑字符串“ 14532465”,然后可能的对是“ 14”,“ 45”,“ 53”,“ 32”,“ 24”,“ 46”,“ 65”。在这些对中,只有“ 32”和“ 24”在转换为整数时可以被4整除。然后,被4整除的子字符串(长度> = 2)必须以“ 32”或“ 24”结尾。 “ 32”是“ 14532”,“ 4532”,“ 532”,“ 32”,即4,索引“ 3”也是4。同样,以“ 24”结尾的子串的数目为5。

这样我们得到一个O(n)解。下面是这种方法的实现。

C++
// C++ program to count number of substrings
// divisible by 4.
#include 
using namespace std;
 
int countDivisbleby4(char s[])
{
    int n = strlen(s);
     
    // In the first loop we will count number of
    // 0's, 4's and 8's present in the string
    int count = 0;
    for (int i = 0; i < n; ++i)
    if (s[i] == '4' || s[i] == '8' || s[i] == '0')
            count++ ;
     
    // In second loop we will convert pairs
    // of two consecutive characters into
    // integer and store it in variable h .
    // Then we check whether h is divisible by 4
    // or not . If h is divisible we increases
    // the count with ( i + 1 ) as index of
    // first character of pair
    for (int i = 0; i < n - 1; ++i) {
    int h = ( s[i] - '0' ) * 10 + ( s[i+1] - '0' );
    if (h % 4 == 0)
        count = count + i + 1 ;
    }
 
    return count;
}
 
// Driver code to test above function
int main()
{
    char s[] = "124";
    cout << countDivisbleby4(s);
    return 0;
}


Java
// Java program to count number of substrings
// divisible by 4
import java.io.*;
 
class GFG
{
    // Function to count number of substrings
    // divisible by 4
    static int countDivisbleby4(String s)
    {
        int n = s.length();
      
        // In the first loop we will count number of
        // 0's, 4's and 8's present in the string
        int count = 0;
        for (int i = 0; i < n; ++i)
            if (s.charAt(i) == '4' || s.charAt(i) == '8' || s.charAt(i) == '0')
                count++ ;
      
        // In second loop we will convert pairs
        // of two consecutive characters into
        // integer and store it in variable h .
        // Then we check whether h is divisible by 4
        // or not . If h is divisible we increases
        // the count with ( i + 1 ) as index of
        // first character of pair
        for (int i = 0; i < n - 1; ++i)
        {
            int h = ( s.charAt(i) - '0' ) * 10 + ( s.charAt(i+1) - '0' );
            if (h % 4 == 0) 
                count = count + i + 1 ;
        }
  
        return count;
    }
     
    // driver program
    public static void main (String[] args)
    {
        String s = "124";
        System.out.println(countDivisbleby4(s));
    }
}
 
// Contributed by Pramod Kumar


Python3
# Python3 program to count the number of substrings
# divisible by 4.
 
def countDivisbleby4(s):
    n = len(s)
     
    # In the first loop we will count number of
    # 0's, 4's and 8's present in the string
    count = 0;
    for i in range(0,n,1):
        if (s[i] == '4' or s[i] == '8' or s[i] == '0'):
            count += 1
     
    # In second loop we will convert pairs
    # of two consecutive characters into
    # integer and store it in variable h .
    # Then we check whether h is divisible by 4
    # or not . If h is divisible we increases
    # the count with ( i + 1 ) as index of
    # first character of pair
    for i in range(0,n - 1,1):
        h = (ord(s[i]) - ord('0')) * 10 + (ord(s[i+1]) - ord('0'))
        if (h % 4 == 0):
            count = count + i + 1
     
    return count
 
# Driver code to test above function
if __name__ == '__main__':
    s = ['1','2','4']
    print(countDivisbleby4(s))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to count number of
// substrings divisible by 4
using System;
     
public class GFG
{
     
    // Function to count number of
    // substrings divisible by 4
    static int countDivisbleby4(string s)
    {
        int n = s.Length;
     
        // In the first loop we will count
        // number of 0's, 4's and 8's present 
        // in the string
        int count = 0;
        for (int i = 0; i < n; ++i)
         
            if (s[i] == '4' || s[i] == '8'
                            || s[i] == '0')
                count++ ;
     
        // In second loop we will convert pairs
        // of two consecutive characters into
        // integer and store it in variable h .
        // Then we check whether h is divisible
        // by 4 or not . If h is divisible, we
        // increases the count with ( i + 1 )
        // as index of first character of pair
        for (int i = 0; i < n - 1; ++i)
        {
            int h = (s[i] - '0' ) * 10 +
                    (s[i + 1] - '0' );
            if (h % 4 == 0)
                count = count + i + 1 ;
        }
 
        return count;
    }
     
    // Driver Code
    public static void Main ()
    {
        string s = "124";
        Console.WriteLine(countDivisbleby4(s));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:

4