I guess people like to read about the things that they want the most and experience the least.

Advertisements

How to Solve “constructSquare” in CodeFights

The Problem:

Given a string consisting of lowercase English letters, find the largest square which can be obtained by reordering its characters and replacing them with digits (leading zeros are not allowed) where same characters always map to the same digits and different characters always map to different digits.

If there is no solution, return -1.

Example

  • For s = "ab", the output should be
    constructSquare(s) = 81;
  • For s = "zzz", the output should be
    constructSquare(s) = -1.

    There are no 3-digit square numbers with identical digits.

  • For s = "aba", the output should be
    constructSquare(s) = 900.

    It can be obtained after reordering the initial string into "baa".

Input/Output

  • [time limit] 4000ms (py)
  • [input] string s

    Constraints:
    2 ≤ s.length < 10.

  • [output] integer

Continue reading “How to Solve “constructSquare” in CodeFights”

How to Solve “minimalNumberOfCoins” in CodeFights

The Problem:

You find yourself in Bananaland trying to buy a banana. You are super rich so you have an unlimited supply of banana-coins, but you are trying to use as few coins as possible.

The coin values available in Bananaland are stored in a sorted array coins. coins[0] = 1, and for each i (0 < i < coins.length) coins[i] is divisible by coins[i - 1]. Find the minimal number of banana-coins you’ll have to spend to buy a banana given the banana’s price.

Example

For coins = [1, 2, 10] and price = 28, the output should be
minimalNumberOfCoins(coins, price) = 6.

You have to use 10 twice, and 2 four times.

Input/Output

  • [time limit] 4000ms (py)
  • [input] array.integer coinsThe coin values available in Bananaland.Constraints:
    1 ≤ coins.length ≤ 5,
    1 ≤ coins[i] ≤ 120.
  • [input] integer priceA positive integer representing the price of the banana.Constraints:
    8 ≤ price ≤ 250.
  • [output] integerThe minimal number of coins you can use to buy the banana.

Continue reading “How to Solve “minimalNumberOfCoins” in CodeFights”

How to Solve “alphabetSubsequence” in CodeFights

The Problem:

Check whether the given string is a subsequence of the plaintext alphabet.

Example

  • For s = "effg" or s = "cdce", the output should be
    alphabetSubsequence(s) = false;
  • For s = "ace" or s = "bxz", the output should be
    alphabetSubsequence(s) = true.

Input/Output

  • [time limit] 4000ms (py)
  • [input] string sConstraints:
    2 ≤ s.length ≤ 15.
  • [output] booleantrue if the given string is a subsequence of the alphabet, false otherwise.

Continue reading “How to Solve “alphabetSubsequence” in CodeFights”

How to Solve “houseOfCats” in CodeFights

The Problem:

There are some people and cats in a house. You are given the number of legs they have all together. Your task is to return an array containing every possible number of people that could be in the house sorted in ascending order. It’s guaranteed that each person has 2 legs and each cat has 4 legs.

Example

  • For legs = 6, the output should be
    houseOfCats(legs) = [1, 3].There could be either 1 cat and 1 person (4 + 2 = 6) or 3 people (2 * 3 = 6).
  • For legs = 2, the output should be
    houseOfCats(legs) = [1].There can be only 1 person.

Input/Output

  • [time limit] 4000ms (py)
  • [input] integer legsThe total number of legs in the house.

    Constraints:
    0 ≤ legs ≤ 45.

  • [output] array.integerEvery possible number of people that can be in the house.

Continue reading “How to Solve “houseOfCats” in CodeFights”

How to Solve “reflectString” in CodeFights

The Problem:

Define an alphabet reflection as follows: a turns into z, b turns into y, c turns into x, …, n turns into m, m turns into n, …, z turns into a.

Define a string reflection as the result of applying the alphabet reflection to each of its characters.

Reflect the given string.

Example

For inputString = "name", the output should be
reflectString(inputString) = "mznv".

Input/Output

  • [time limit] 4000ms (py)
  • [input] string inputString

    A string of lowercase characters.

    Constraints:
    3 ≤ inputString.length ≤ 10.

  • [output] string

Continue reading “How to Solve “reflectString” in CodeFights”

How to Solve “allLongestStrings” in CodeFights

The Problem:

Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

Input/Output

  • [time limit] 4000ms (py)
  • [input] array.string inputArray

    A non-empty array.

    Constraints:
    1 ≤ inputArray.length ≤ 10,
    1 ≤ inputArray[i].length ≤ 10.

  • [output] array.string

    Array of the longest strings, stored in the same order as in the inputArray.

Continue reading “How to Solve “allLongestStrings” in CodeFights”

How to Solve “isSubstitutionCipher” in CodeFights

The Problem:

A ciphertext alphabet is obtained from the plaintext alphabet by means of rearranging some characters. For example "bacdef...xyz" will be a simple ciphertext alphabet where a and b are rearranged.

A substitution cipher is a method of encoding where each letter of the plaintext alphabet is replaced with the corresponding (i.e. having the same index) letter of some ciphertext alphabet.

Given two strings, check whether it is possible to obtain them from each other using some (possibly, different) substitution ciphers.

Example

  • For string1 = "aacb" and string2 = "aabc", the output should be
    isSubstitutionCipher(string1, string2) = true.

    Any ciphertext alphabet that starts with acb... would make this transformation possible.

  • For string1 = "aa" and string2 = "bc", the output should be
    isSubstitutionCipher(string1, string2) = false.

Input/Output

  • [time limit] 4000ms (py)
  • [input] string string1

    A string consisting of lowercase characters.

    Constraints:
    1 ≤ string1.length ≤ 10.

  • [input] string string2

    A string consisting of lowercase characters of the same length as string1.

    Constraints:
    string2.length = strin1.length.

  • [output] boolean

Continue reading “How to Solve “isSubstitutionCipher” in CodeFights”