Given a string S representing a Morse Code, the duty is to test is the code is legitimate or not. A Morse code is legitimate if that meets all of the under necessities:
- Any message should start with a dot. [ ‘.’ ]
- Any message should finish with a splash. [ ‘-‘ ]
- Each dot should have a corresponding sprint after it to shut it.
Examples:
Enter: S = “.–“
Output: VaildEnter: S = “.”
Output: InvalidEnter: S = “-“
Output: Invalid
Method: It is a easy implementation based mostly downside the place the primary, final and every pair of characters must be checked for the given situations. Observe the given steps to unravel the issue:
- If the primary or final characters will not be dot and sprint respectively then the string is invalid.
- Traverse the message from 0 to N-1:
- If the character at index i is a dot however at index i+1 shouldn’t be a sprint (-), then the code is invalid.
- If the loop ends, means the message has met all the necessities. So the code is legitimate.
Beneath is the implementation for the above strategy:
C++14
|
Time Complexity: O(N)
Auxiliary Area: O(1)