How to resolve the algorithm ABC problem step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm ABC problem step by step in the Lua programming language

Table of Contents

Problem Statement

You are given a collection of ABC blocks   (maybe like the ones you had when you were a kid).
There are twenty blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:

Write a function that takes a string (word) and determines whether the word can be spelled with the given collection of blocks.

The rules are simple:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm ABC problem step by step in the Lua programming language

Source code in the lua programming language

blocks = {
	{"B","O"};	{"X","K"};	{"D","Q"};	{"C","P"};
	{"N","A"};	{"G","T"};	{"R","E"};	{"T","G"};
	{"Q","D"};	{"F","S"};	{"J","W"};	{"H","U"};
	{"V","I"};	{"A","N"};	{"O","B"};	{"E","R"};
	{"F","S"};	{"L","Y"};	{"P","C"};	{"Z","M"};
	};

function canUse(table, letter)
	for i,v in pairs(blocks) do
		if (v[1] == letter:upper() or v[2] == letter:upper())  and table[i] then
			table[i] = false;
			return true;
		end
	end
	return false;
end

function canMake(Word)
	local Taken = {};
	for i,v in pairs(blocks) do
		table.insert(Taken,true);
	end
	local found = true;
	for i = 1,#Word do
		if not canUse(Taken,Word:sub(i,i)) then
			found = false;
		end
	end
	print(found)
end


  

You may also check:How to resolve the algorithm File size step by step in the K programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Ursa programming language
You may also check:How to resolve the algorithm Classes step by step in the COBOL programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Nim programming language
You may also check:How to resolve the algorithm URL parser step by step in the JavaScript programming language