Check if given Morse Code is valid
Last Updated :
18 Aug, 2022
Given a string S representing a Morse Code, the task is to check is the code is valid or not. A Morse code is valid if that meets all the below requirements:
- Any message must begin with a dot. [ ‘.’ ]
- Any message must end with a dash. [ ‘-‘ ]
- Every dot must have a corresponding dash after it to close it.
Examples:
Input: S = “.–“
Output: Valid
Input: S = “.”
Output: Invalid
Input: S = “-“
Output: Invalid
Approach: This is a simple implementation based problem where the first, last and each pair of characters need to be checked for the given conditions. Follow the given steps to solve the problem:
- If the first or last characters are not dot and dash respectively then the string is invalid.
- Traverse the message from 0 to N-1:
- If the character at index i is a dot but at index i+1 is not a dash (-), then the code is invalid.
- If the loop ends, means the message has met all the requirements. So the code is valid.
Below is the implementation for the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
bool isValidMorse(string& code)
{
int n = code.length();
if (code[0] != '.' || code[n - 1] != '-' )
return 0;
for ( int i = 0; i < n - 1; i++) {
if (code[i] == '.' && code[i + 1] != '-' )
return 0;
}
return 1;
}
int main()
{
string code = ".--" ;
if (isValidMorse(code))
cout << "Valid" ;
else
cout << "Invalid" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isValidMorse(String code)
{
int n = code.length();
if (code.charAt( 0 ) != '.' || code.charAt(n - 1 ) != '-' )
return false ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (code.charAt(i) == '.' && code.charAt(i + 1 ) != '-' )
return false ;
}
return true ;
}
public static void main(String args[])
{
String code = ".--" ;
if (isValidMorse(code))
System.out.println( "Valid" );
else
System.out.println( "Invalid" );
}
}
|
Python3
def isValidMorse(code):
n = len (code)
if (code[ 0 ] ! = '.' or code[n - 1 ] ! = '-' ):
return 0
for i in range (n - 1 ):
if (code[i] = = '.' and code[i + 1 ] ! = '-' ):
return 0
return 1
code = ".--"
if (isValidMorse(code)):
print ( "Valid" )
else :
print ( "Invalid" )
|
C#
using System;
public class GFG
{
static bool isValidMorse(String code)
{
int n = code.Length;
if (code[0] != '.' || code[(n - 1)] != '-' )
return false ;
for ( int i = 0; i < n - 1; i++) {
if (code[i] == '.' && code[(i + 1)] != '-' )
return false ;
}
return true ;
}
public static void Main(String []args)
{
String code = ".--" ;
if (isValidMorse(code))
Console.WriteLine( "Valid" );
else
Console.WriteLine( "Invalid" );
}
}
|
Javascript
<script>
function isValidMorse(code) {
let n = code.length;
if (code[0] != '.' || code[n - 1] != '-' )
return 0;
for (let i = 0; i < n - 1; i++) {
if (code[i] == '.' && code[i + 1] != '-' )
return 0;
}
return 1;
}
let code = ".--" ;
if (isValidMorse(code))
document.write( "Valid" );
else
document.write( "Invalid" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...