Answer the question
In order to leave comments, you need to log in
Proposal of an algorithm for solving a 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));
const readline = require('readline')
const rl = readline.createInterface(process.stdin, process.stdout);
rl.on('line', (line) => {
function result(line){
let arr = line.split(' ');
let word1 = arr[0].split('');
let word2 = arr[1].split('');
if(word1.length != word2.length){
return 0;
} else {
for(let i = 0; i < word1.length; i++){
for(let j = 0; j < word1.length; j++){
if (word1[i] == word1[j] && word2[i] != word2[j]){
return 0;
}
}
}
return 1;
}
}
console.log(String(result(line)));
rl.close();
return;
}).on('close', () => process.exit(0));
Answer the question
In order to leave comments, you need to log in
Your solution doesn't work on the "ab aa" example.
It is necessary that different or identical letters be in the same places in both words. You only check that there is no such thing that the letters in the first word are equal, and in the second they are not. We need to check and vice versa.
But your solution does not yet consider an extreme case - all 33 letters of the alphabet are used and there is something that needs to be changed. Here the answer is 0, because there is no way to make replacements - after any replacement, 2 different types of letters will become the same and mix up. You can't separate them after that.
If you fix these jambs, your solution may not pass in time, because you have it for a square. The best solution is to go through the lines at the same time in one loop and remember in a character-indexed array (or map) which letter of the alphabet in the second word corresponds to the letter of the alphabet in the first word. If you encounter a contradiction (something is already written in the array that is not the same as you see now), then the answer is 0.
And one more thing. No need to split lines with .split('')
. In JavaScript, you can find out the length of strings and access the i-th character without converting to an array.
I didn’t particularly read well, judging by the second example, since d was replaced with me, which means we can’t immediately change to the same letter that is in both words. So if we have a constraint equal to the alphabet, then if we submit to the input
"абвгдежзийклмнопрстуфхцчшщъыьэюя яюэьыъщшчцхфутсрпонмлкйизжедгвба"
, then it should return 0. function result(line) {
let arr = line.split(' ');
let word1 = arr[0].split('');
let word2 = arr[1].split('');
if (word1.length != word2.length)
return 0;
var abc = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
for (var i = 0; i < word1.length; i++)
if (!word1.every((e, j) => (e !== word1[i]) || (e == word1[i] && word2[j] == word2[i])))
return 0;
var i = 0;
while (word1.join("") != word2.join("")) {
if (word1[i] != word2[i]) {
var repS = word2[i];
if (word1.join("").indexOf(repS) != -1) {
var tempAbc = abc.split("").filter(w => word1.join("").indexOf(w) == -1);
if (tempAbc.length == 0)
return 0;
repS = tempAbc.pop();
}
word1 = word1.join("").replace(new RegExp(word1[i], "g"), repS).split("");
console.log(word1)
}
i = (i + 1) % word1.length;
}
return 1;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question