📜  算上对字符的字符串,其ASCII值之差为K

📅  最后修改于: 2021-04-24 17:00:03             🧑  作者: Mango

给定一个由小写字母组成的字符串str和一个非负整数K。任务是找到给定字符串ASCII值恰好为K的字符对数。

例子:

方法:将每个字符的频率存储在一个数组中。遍历此频率阵列以获得所需的答案。存在两种情况:

  1. 如果K = 0,则检查相似字符出现多次(即freq [i]> 1) 。如果是,则将(freq [i] *(freq [i] – 1))/ 2加到计数中。
  2. 如果K!= 0,则检查是否存在两个具有ASCII值差异的字符,如K所说的freq [i]freq [j] 。然后将freq [i] * freq [j]添加到计数中。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 26
  
// Function to return the count of
// required pairs of characters
int countPairs(string str, int k)
{
  
    // Length of the string
    int n = str.size();
  
    // To store the frequency
    // of each character
    int freq[MAX];
    memset(freq, 0, sizeof freq);
  
    // Update the frequency
    // of each character
    for (int i = 0; i < n; i++)
        freq[str[i] - 'a']++;
  
    // To store the required
    // count of pairs
    int cnt = 0;
  
    // If ascii value difference is zero
    if (k == 0) {
  
        // If there exists similar characters
        // more than once
        for (int i = 0; i < MAX; i++)
            if (freq[i] > 1)
                cnt += ((freq[i] * (freq[i] - 1)) / 2);
    }
    else {
  
        // If there exits characters with
        // ASCII value difference as k
        for (int i = 0; i < MAX; i++)
            if (freq[i] > 0 && i + k < MAX && freq[i + k] > 0)
                cnt += (freq[i] * freq[i + k]);
        ;
    }
  
    // Return the required count
    return cnt;
}
  
// Driver code
int main()
{
    string str = "abcdab";
    int k = 0;
  
    cout << countPairs(str, k);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
  
class GFG 
{
  
    static int MAX = 26;
  
    // Function to return the count of
    // required pairs of characters
    static int countPairs(char[] str, int k) 
    {
  
        // Length of the string
        int n = str.length;
  
        // To store the frequency
        // of each character
        int[] freq = new int[MAX];
  
        // Update the frequency
        // of each character
        for (int i = 0; i < n; i++) 
        {
            freq[str[i] - 'a']++;
        }
  
        // To store the required
        // count of pairs
        int cnt = 0;
  
        // If ascii value difference is zero
        if (k == 0)
        {
  
            // If there exists similar characters
            // more than once
            for (int i = 0; i < MAX; i++)
            {
                if (freq[i] > 1) 
                {
                    cnt += ((freq[i] * (freq[i] - 1)) / 2);
                }
            }
        } 
        else
        {
  
            // If there exits characters with
            // ASCII value difference as k
            for (int i = 0; i < MAX; i++)
            {
                if (freq[i] > 0 && i + k < MAX && freq[i + k] > 0)
                {
                    cnt += (freq[i] * freq[i + k]);
                }
            }
            ;
        }
  
        // Return the required count
        return cnt;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String str = "abcdab";
        int k = 0;
  
        System.out.println(countPairs(str.toCharArray(), k));
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach 
MAX = 26
  
# Function to return the count of 
# required pairs of characters 
def countPairs(string, k) : 
  
    # Length of the string 
    n = len(string); 
  
    # To store the frequency 
    # of each character 
    freq = [0] * MAX; 
  
    # Update the frequency 
    # of each character 
    for i in range(n) : 
        freq[ord(string[i]) - 
             ord('a')] += 1; 
  
    # To store the required 
    # count of pairs 
    cnt = 0; 
  
    # If ascii value difference is zero 
    if (k == 0) :
  
        # If there exists similar characters 
        # more than once 
        for i in range(MAX) :
            if (freq[i] > 1) :
                cnt += ((freq[i] * 
                        (freq[i] - 1)) // 2); 
      
    else :
  
        # If there exits characters with 
        # ASCII value difference as k 
        for i in range(MAX) :
            if (freq[i] > 0 and 
                i + k < MAX and 
                freq[i + k] > 0) :
                cnt += (freq[i] * freq[i + k]); 
  
    # Return the required count 
    return cnt; 
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "abcdab"; 
    k = 0; 
  
    print(countPairs(string, k)); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
    static int MAX = 26;
  
    // Function to return the count of
    // required pairs of characters
    static int countPairs(char[] str, int k) 
    {
  
        // Length of the string
        int n = str.Length;
  
        // To store the frequency
        // of each character
        int[] freq = new int[MAX];
  
        // Update the frequency
        // of each character
        for (int i = 0; i < n; i++) 
        {
            freq[str[i] - 'a']++;
        }
  
        // To store the required
        // count of pairs
        int cnt = 0;
  
        // If ascii value difference is zero
        if (k == 0)
        {
  
            // If there exists similar characters
            // more than once
            for (int i = 0; i < MAX; i++)
            {
                if (freq[i] > 1) 
                {
                    cnt += ((freq[i] * (freq[i] - 1)) / 2);
                }
            }
        } 
        else
        {
  
            // If there exits characters with
            // ASCII value difference as k
            for (int i = 0; i < MAX; i++)
            {
                if (freq[i] > 0 && i + k < MAX && freq[i + k] > 0)
                {
                    cnt += (freq[i] * freq[i + k]);
                }
            }
            ;
        }
  
        // Return the required count
        return cnt;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        String str = "abcdab";
        int k = 0;
  
        Console.WriteLine(countPairs(str.ToCharArray(), k));
    }
}
  
// This code has been contributed by 29AjayKumar


输出:
2