Decode the Secret Heist Code
Last Updated :
19 Oct, 2023
Berlin and Professor are on a secret heist mission, They have a secret code that’s the code which will signal Berlin to enter the building and initiate the heist. The plan is that the professor will write a word of random characters on his board. If removing characters from the word will give the secret code then it’s time for Berlin to shine (initiate the heist) but remember If one character is removed then it’s all existences in the word will be removed.
As Berlin is short temper he can initiate the mission if he is unable to decode the word, that’s why the professor needs your help to write a code that will take a word, the word written on the board, and the code, the secret code for the signal and return “Yes” if it’s time to initiate the mission or “No” if he still needs to wait.
Examples:
Input: word = “bhbalcbdkqgegr”, code = “hacker”
Output: Yes
Explanation: If we remove all existence of b, l, d, q, and g .we will get our code “hacker”
Input: word = “acheranobylkl”, code = “chernobyl“
Output: No
Explanation: If we remove all existence of a and k, we will get “chernobyll” which has an extra “l” at the end which cannot be removed, as removing that will remove all existence of “l”.
Naive Approach: The basic way to solve the problem is as follows:
The idea is to iterate through the string “word” and whenever we get a character that is also present in the “code” we will add it to the “decoded” string. After the iteration, if the decoded string comes exactly the same as the code then we will return “Yes” otherwise “No”.
Below is the implementation of the above idea.
C++
#include <iostream>
#include <string>
using namespace std;
string decoding(string word, string code)
{
string decoded = "" ;
for ( char i : word) {
if (code.find(i) != string::npos) {
decoded += i;
}
}
if (decoded == code) {
return "YES" ;
}
else {
return "NO" ;
}
}
int main()
{
string word = "bhbalcbdkqgegr" ;
string code = "hacker" ;
cout << decoding(word, code) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
String word = "bhbalcbdkqgegr" ;
String code = "hacker" ;
System.out.println(decoding(word, code));
}
public static String decoding(String word, String code)
{
String decoded = "" ;
for ( char i : word.toCharArray()) {
if (code.indexOf(i) != - 1 ) {
decoded += i;
}
}
if (decoded.equals(code)) {
return "YES" ;
}
else {
return "NO" ;
}
}
}
|
Python3
def decoding(word, code):
decoded = ""
for i in word:
if i in code:
decoded + = i
if decoded = = code:
return "YES"
else :
return "NO"
word = "bhbalcbdkqgegr"
code = "hacker"
print (decoding(word, code))
|
C#
using System;
public class GFG {
public static void Main( string [] args)
{
string word = "bhbalcbdkqgegr" ;
string code = "hacker" ;
Console.WriteLine(decoding(word, code));
}
public static string decoding( string word, string code)
{
string decoded = "" ;
foreach ( char i in word.ToCharArray())
{
if (code.IndexOf(i) != -1) {
decoded += i;
}
}
if (decoded.Equals(code)) {
return "YES" ;
}
else {
return "NO" ;
}
}
}
|
Javascript
function decoding(word, code) {
let decoded = "" ;
for (let i of word) {
if (code.includes(i)) {
decoded += i;
}
}
if (decoded === code) {
return "YES" ;
}
else {
return "NO" ;
}
}
let word = "bhbalcbdkqgegr" ;
let code = "hacker" ;
console.log(decoding(word, code));
|
Time Complexity: O(N),
Auxiliary Space: O(N), where N is the length of the word string.
Efficient Approach: To solve the problem using Two Pointers follow the below idea:
In place of storing the value that matches the “code,” we will use a second pointer which will be shifted as we iterate through the “word” string until we reach the end of “word” or “code”.
Below is the implementation of the above idea.
C++
#include <iostream>
#include <string>
using namespace std;
string decoding(string word, string code) {
int word_index = 0;
int code_index = 0;
while (word_index < word.length() && code_index < code.length()) {
if (word[word_index] == code[code_index]) {
word_index++;
code_index++;
} else {
word_index++;
}
}
if (word_index == word.length()) {
if (code_index == code.length()) {
return "YES" ;
} else {
return "NO" ;
}
} else {
while (word_index < word.length()) {
if (code.find(word[word_index]) != string::npos) {
return "NO" ;
}
word_index++;
}
return "YES" ;
}
}
int main() {
string word = "bhbalcbdkqgegr" ;
string code = "hacker" ;
cout << decoding(word, code) << endl;
return 0;
}
|
Java
public class GFG {
public static String decoding(String word, String code) {
int wordIndex = 0 ;
int codeIndex = 0 ;
while (wordIndex < word.length() && codeIndex < code.length()) {
if (word.charAt(wordIndex) == code.charAt(codeIndex)) {
wordIndex++;
codeIndex++;
} else {
wordIndex++;
}
}
if (wordIndex == word.length()) {
if (codeIndex == code.length()) {
return "YES" ;
} else {
return "NO" ;
}
} else {
while (wordIndex < word.length()) {
if (code.contains(String.valueOf(word.charAt(wordIndex)))) {
return "NO" ;
}
wordIndex++;
}
return "YES" ;
}
}
public static void main(String[] args) {
String word = "bhbalcbdkqgegr" ;
String code = "hacker" ;
System.out.println(decoding(word, code));
}
}
|
Python3
def decoding(word, code):
word_index = 0
code_index = 0
while word_index < len (word) and code_index < len (code):
if word[word_index] = = code[code_index]:
word_index + = 1
code_index + = 1
else :
word_index + = 1
if word_index = = len (word):
if code_index = = len (code):
return "YES"
else :
return "NO"
else :
while word_index < len (word):
if word[word_index] in code:
return "NO"
word_index + = 1
return "YES"
word = "bhbalcbdkqgegr"
code = "hacker"
print (decoding(word, code))
|
C#
using System;
class Program
{
static string Decoding( string word, string code)
{
int wordIndex = 0;
int codeIndex = 0;
while (wordIndex < word.Length && codeIndex < code.Length)
{
if (word[wordIndex] == code[codeIndex])
{
wordIndex++;
codeIndex++;
}
else
{
wordIndex++;
}
}
if (wordIndex == word.Length)
{
if (codeIndex == code.Length)
{
return "YES" ;
}
else
{
return "NO" ;
}
}
else
{
while (wordIndex < word.Length)
{
if (code.Contains(word[wordIndex].ToString()))
{
return "NO" ;
}
wordIndex++;
}
return "YES" ;
}
}
static void Main()
{
string word = "bhbalcbdkqgegr" ;
string code = "hacker" ;
Console.WriteLine(Decoding(word, code));
Console.ReadLine();
}
}
|
Javascript
function decoding(word, code) {
let wordIndex = 0;
let codeIndex = 0;
while (wordIndex < word.length && codeIndex < code.length) {
if (word[wordIndex] === code[codeIndex]) {
wordIndex++;
codeIndex++;
} else {
wordIndex++;
}
}
if (wordIndex === word.length) {
if (codeIndex === code.length) {
return "YES" ;
} else {
return "NO" ;
}
} else {
while (wordIndex < word.length) {
if (code.includes(word[wordIndex])) {
return "NO" ;
}
wordIndex++;
}
return "YES" ;
}
}
const word = "bhbalcbdkqgegr" ;
const code = "hacker" ;
console.log(decoding(word, code));
|
Time Complexity: O(N), where N is the length of the word string.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...