D
D
DarkOracleLex2020-09-29 17:16:22
JavaScript
DarkOracleLex, 2020-09-29 17:16:22

What am I doing wrong in the test task?

Problem condition
Time limit, s1
Memory limit, MB64

2 lines are fed as input. It is necessary to determine whether it is possible to turn the first line into the second, replacing one letter with another, taking into account the following rules:

- only the letters of the Russian alphabet а-z are involved;
- all letters in lower case;
- in one step, you can convert all occurrences of one letter to another.

Input Data
The input information comes from the standard input as a single line. This string contains two substrings separated by a space. Your decision should take into account the variant when strings of different lengths are given as input. Incorrect input data is not received, additional checks are not required.

Output
As a response to the standard output, the program should output 1 (if it can be converted) or 0 (if it cannot be converted).

Example 1

Input:
hello fun
Output:
1 Transformation (no need to output):
to ⇒ k (prick)
e ⇒ o (prick)
t ⇒ l (fun)

Example 2

Input:
aabddd ddbbaa
Output:
1 Transform (output not needed): d
⇒ i (aabbya) a
⇒ d (ddbbya) i
⇒ a ( ddbbaa

) X'.


Solution Design Notes

When submitting solutions to Java, you must name the executable class Main. The solution does not need to specify a package.
To work with standard input in JS, use require('readline'), and to work with standard output, use console.log(String(data)).

JS I/O example:

const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
rl.on('line', (line) => {
    // Введенная строка в переменной line, тут можно написать решение
    console.log(String(result));
    rl.close();
    return;
}).on('close', () => process.exit(0));


My decision:
const readline = require("readline");
const rl = readline.createInterface(
  process.stdin,
  process.stdout
);
rl.on("line", (line) => {
  function isConvertible(str) {
    let modifiableStr = str.split(" ")[0],
      modifierStr = str.split(" ")[1];

    if (modifiableStr.length !== modifierStr.length) {
      return 0;
    } else {
      const map1 = new Map(),
        map2 = new Map();

      for (let i = 0; i < modifiableStr.length; i++) {
        if (
          (map1.has(modifiableStr[i]) &&
            map1.get(modifiableStr[i]) !== modifierStr[i]) ||
          (map2.has(modifierStr[i]) &&
            map2.get(modifierStr[i]) !== modifiableStr[i])
        ) {
          return 0;
          }
        map1.set(modifiableStr[i], modifierStr[i]);
        map2.set(modifierStr[i], modifiableStr[i]);
      }

      if (map1.size === 33) {
        return 0;
      }
    }
    return 1;
  }
  console.log(String(isConvertible(line)));
  rl.close();
  return;
}).on("close", () => process.exit(0));


I checked, like
console.log(isConvertible("привет прикол"));
console.log(isConvertible("ааббдд ддббаа"));
console.log(isConvertible("абаб ааах"));
console.log(isConvertible("алло яспав"));
console.log(isConvertible("аб аа"));
console.log(isConvertible("аб aб"));
console.log(
  isConvertible(
    "абвгдеёжзийклмнопрстуфхцчшщъыьэюя яюэьыъщшчцхфутсрпонмлкйизжёедгвба"
  )
);

gives correct values.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-09-29
@DarkOracleLex

How to convert words?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question