📌  相关文章
📜  通过用莫尔斯电码替换每个字符来计算可能的不同字符串

📅  最后修改于: 2021-09-06 06:33:24             🧑  作者: Mango

给定一个字符串数组arr[] ,任务是通过用摩尔斯电码替换字符串的每个字符来计算可以从给定数组生成的不同字符串的数量。以下是所有小写字母的摩尔斯电码:

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个数组,比如说morseCode[]来存储所有小写字符的莫尔斯电码。
  • 创建一个集合,比如st ,通过将每个字符替换为其莫尔斯电码来存储数组的不同元素。
  • 遍历数组并将数组字符串的莫尔斯电码插入到st 中
  • 最后,打印st 中存在的元素计数。

下面是上述方法的实现。

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count unique array elements
// by replacing each character by its Morse code
int uniqueMorseRep(vector& arr)
{
 
    // Stores Morse code of all
    // lowercase characters
    vector morseCode
        = {
            ".-", "-...", "-.-.",
            "-..", ".", "..-.", "--.",
            "....", "..", ".---", "-.-",
            ".-..", "--", "-.", "---",
            ".--.", "--.-", ".-.", "...",
            "-", "..-", "...-", ".--",
            "-..-", "-.--", "--.."
          };
 
    // Stores distinct elements of string by
    // replacing each character by Morse code
    set st;
 
    // Stores length of arr[] array
    int N = arr.size();
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Stores the Morse code
        // of arr[i]
        string temp = "";
 
        // Stores length of
        // current string
        int M = arr[i].length();
 
        for (int j = 0; j < M; j++) {
 
            // Update temp
            temp += morseCode[arr[i][j] - 'a'];
        }
 
        // Insert temp into st
        st.insert(temp);
    }
 
    // Return count of elements
    // in the set
    return st.size();
}
 
// Driver code
int main()
{
    vector arr = { "gig", "zeg",
                           "gin", "msn" };
    cout << uniqueMorseRep(arr) << endl;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to count unique
// array elements by replacing
// each character by its Morse code
static int uniqueMorseRep(String[] arr)
{
  // Stores Morse code of all
  // lowercase characters
  String []morseCode = {".-", "-...", "-.-.",
                        "-..", ".", "..-.", "--.",
                        "....", "..", ".---", "-.-",
                        ".-..", "--", "-.", "---",
                        ".--.", "--.-", ".-.", "...",
                        "-", "..-", "...-", ".--",
                        "-..-", "-.--", "--.."};
 
  // Stores distinct elements of
  // String by replacing each
  // character by Morse code
  HashSet st = new HashSet<>();
 
  // Stores length of arr[] array
  int N = arr.length;
 
  // Traverse the array
  for (int i = 0; i < N; i++)
  {
    // Stores the Morse code
    // of arr[i]
    String temp = "";
 
    // Stores length of
    // current String
    int M = arr[i].length();
 
    for (int j = 0; j < M; j++)
    {
      // Update temp
      temp += morseCode[arr[i].charAt(j) - 'a'];
    }
 
    // Insert temp into st
    st.add(temp);
  }
 
  // Return count of elements
  // in the set
  return st.size();
}
 
// Driver code
public static void main(String[] args)
{
  String[] arr = {"gig", "zeg",
                  "gin", "msn"};
  System.out.print(uniqueMorseRep(arr) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to count unique
# array elements by replacing
# each character by its Morse
# code
def uniqueMorseRep(arr):
   
    # Stores Morse code of
    # all lowercase characters
    morseCode = [".-", "-...", "-.-.",
                 "-..", ".", "..-.",
                 "--.", "....", "..",
                 ".---", "-.-", ".-..",
                 "--", "-.", "---", ".--.",
                 "--.-", ".-.", "...", "-",
                 "..-", "...-", ".--", "-..-",
                 "-.--", "--.."];
 
    # Stores distinct elements of
    # String by replacing each
    # character by Morse code
    st = set();
 
    # Stores length of arr array
    N = len(arr);
 
    # Traverse the array
    for i in range(N):
       
        # Stores the Morse code
        # of arr[i]
        temp = "";
 
        # Stores length of
        # current String
        M = len(arr[i]);
 
        for j in range(M):
           
            # Update temp
            temp += morseCode[ord(arr[i][j]) -
                              ord('a')];
 
        # Insert temp into st
        st.add(temp);
 
    # Return count of elements
    # in the set
    return len(st);
 
# Driver code
if __name__ == '__main__':
   
    arr = ["gig", "zeg",
           "gin", "msn"];
    print(uniqueMorseRep(arr) , "");
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to count unique
// array elements by replacing
// each character by its Morse code
static int uniqueMorseRep(String[] arr)
{
  // Stores Morse code of all
  // lowercase characters
  String []morseCode = {".-", "-...", "-.-.",
                        "-..", ".", "..-.", "--.",
                        "....", "..", ".---", "-.-",
                        ".-..", "--", "-.", "---",
                        ".--.", "--.-", ".-.", "...",
                        "-", "..-", "...-", ".--",
                        "-..-", "-.--", "--.."};
 
  // Stores distinct elements of
  // String by replacing each
  // character by Morse code
  HashSet st = new HashSet();
 
  // Stores length of []arr array
  int N = arr.Length;
 
  // Traverse the array
  for (int i = 0; i < N; i++)
  {
    // Stores the Morse code
    // of arr[i]
    String temp = "";
 
    // Stores length of
    // current String
    int M = arr[i].Length;
 
    for (int j = 0; j < M; j++)
    {
      // Update temp
      temp += morseCode[arr[i][j] - 'a'];
    }
 
    // Insert temp into st
    st.Add(temp);
  }
 
  // Return count of elements
  // in the set
  return st.Count;
}
 
// Driver code
public static void Main(String[] args)
{
  String[] arr = {"gig", "zeg",
                  "gin", "msn"};
  Console.Write(uniqueMorseRep(arr) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
2

时间复杂度: O(N×M) ,其中 N 是数组的大小,M 是单词的长度。
辅助空间: O(N)

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