📌  相关文章
📜  表示给定字符串所需的棒数

📅  最后修改于: 2021-10-27 07:04:33             🧑  作者: Mango

给定一个由大写字母和数字组成的字符串str ,任务是找到表示它所需的火柴棍数量。

例子:

方法:

  1. 这个想法是存储表示特定字母和数字所需的火柴棒计数,如上图所示。
  2. 遍历给定的字符串str并添加每个字符所需的火柴棒计数。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
#include 
using namespace std;
 
// stick[] stores the count
// of matchsticks required to
// represent the alphabets
int sticks[] = { 6, 7, 4, 6, 5, 4, 6,
                 5, 2, 4, 4, 3, 6, 6,
                 6, 5, 7, 6, 5, 3, 5,
                 4, 6, 4, 3, 4 };
 
// number[] stores the count
// of matchsticks required to
// represent the numerals
int number[] = { 6, 2, 5, 5, 4, 5, 6,
                 3, 7, 6 };
 
// Function that return the count of
// sticks required to represent
// the given string
int countSticks(string str)
{
    int cnt = 0;
 
    // For every char of the given
    // string
    for (int i = 0; str[i]; i++) {
 
        char ch = str[i];
 
        // Add the count of sticks
        // required to represent the
        // current character
        if (ch >= 'A' && ch <= 'Z') {
            cnt += sticks[ch - 'A'];
        }
        else {
            cnt += number[ch - '0'];
        }
    }
    return cnt;
}
 
// Driver code
int main()
{
    string str = "GEEKSFORGEEKS";
 
    // Function call to find the
    // count of matchsticks
    cout << countSticks(str);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG {
     
    // stick[] stores the count
    // of matchsticks required to
    // represent the alphabets
    static int sticks[] = { 6, 7, 4, 6, 5, 4, 6,
                     5, 2, 4, 4, 3, 6, 6,
                     6, 5, 7, 6, 5, 3, 5,
                     4, 6, 4, 3, 4 };
     
    // number[] stores the count
    // of matchsticks required to
    // represent the numerals
    static int number[] = { 6, 2, 5, 5, 4, 5, 6,
                     3, 7, 6 };
     
    // Function that return the count of
    // sticks required to represent
    // the given string
    static int countSticks(String str)
    {
        int cnt = 0;
     
        // For every char of the given
        // string
        for (int i = 0; i < str.length(); i++) {
     
            char ch = str.charAt(i);
     
            // Add the count of sticks
            // required to represent the
            // current character
            if (ch >= 'A' && ch <= 'Z') {
                cnt += sticks[ch - 'A'];
            }
            else {
                cnt += number[ch - '0'];
            }
        }
        return cnt;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String str = "GEEKSFORGEEKS";
     
        // Function call to find the
        // count of matchsticks
        System.out.println(countSticks(str));
     
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the above approach
 
# stick[] stores the count
# of matchsticks required to
# represent the alphabets
sticks = [ 6, 7, 4, 6, 5, 4, 6,
            5, 2, 4, 4, 3, 6, 6,
            6, 5, 7, 6, 5, 3, 5,
            4, 6, 4, 3, 4 ];
 
# number[] stores the count
# of matchsticks required to
# represent the numerals
number = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ];
 
# Function that return the count of
# sticks required to represent
# the given string
def countSticks(string) :
 
    cnt = 0;
 
    # For every char of the given
    # string
    for i in range(len(string)) :
 
        ch = string[i];
 
        # Add the count of sticks
        # required to represent the
        # current character
        if (ch >= 'A' and ch <= 'Z') :
            cnt += sticks[ord(ch) - ord('A')];
         
        else :
            cnt += number[ord(ch) - ord('0')];
     
    return cnt;
 
# Driver code
if __name__ == "__main__" :
 
    string = "GEEKSFORGEEKS";
 
    # Function call to find the
    # count of matchsticks
    print(countSticks(string));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
    // stick[] stores the count
    // of matchsticks required to
    // represent the alphabets
    static int []sticks = { 6, 7, 4, 6, 5, 4, 6,
                    5, 2, 4, 4, 3, 6, 6,
                    6, 5, 7, 6, 5, 3, 5,
                    4, 6, 4, 3, 4 };
     
    // number[] stores the count
    // of matchsticks required to
    // represent the numerals
    static int []number = { 6, 2, 5, 5, 4, 5, 6,
                    3, 7, 6 };
     
    // Function that return the count of
    // sticks required to represent
    // the given string
    static int countSticks(string str)
    {
        int cnt = 0;
     
        // For every char of the given
        // string
        for (int i = 0; i < str.Length; i++)
        {
     
            char ch = str[i];
     
            // Add the count of sticks
            // required to represent the
            // current character
            if (ch >= 'A' && ch <= 'Z')
            {
                cnt += sticks[ch - 'A'];
            }
            else
            {
                cnt += number[ch - '0'];
            }
        }
        return cnt;
    }
     
    // Driver code
    public static void Main()
    {
        string str = "GEEKSFORGEEKS";
     
        // Function call to find the
        // count of matchsticks
        Console.WriteLine(countSticks(str));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
66

时间复杂度: O(N),其中 N 是给定字符串的长度。