T
T
Tushe2020-09-26 17:09:42
JavaScript
Tushe, 2020-09-26 17:09:42

How to convert words?

Hello!
This problem has already been raised on the forum, these topics have been read and, unfortunately, did not help.
The task itself:
Task condition

Time limit, s 1
Memory limit, MB 64
Total number of sending attempts 15

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

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

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 can't 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 necessary):
d ⇒ i (aabbya) a
⇒ d (ddbbya) i
⇒ a ( ddbbaa

) 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) => {




// Entered string in the line variable, here you can write the solution
console.log(String(result));
rl.close();
return;
}).on('close', () => process.exit(0));

My intended solution:

const readline = require("readline");
const rl = readline.createInterface(process.stdin, process.stdout);
rl.on("line", (line) => {



let result = 1;
let counter = 0;
let counterArr = 33;
let lowline = line.toLowerCase();
var arr = ["а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я"];
let wordLength = (lowline.length - 1) / 2;
let fullHouse = 0;

if ((lowline.length % 2 == 1) && (lowline[wordLength] == '  ')) { 
  result = 1;
}
else { result = 0;} //проверка на одинаковую длину

for (let i = 1; i <= wordLength; i++) {
    if (lowline[i - 1] == lowline[i + wordLength]) {
        counter++
    }
    if (counter == wordLength) {
         return result = 1;
    }
} //проверка на одинаковые слова



if (wordLength >= 33) {
for (let y = 1; y <= wordLength && result > 0; y++) {
       for (let i = 0; i <= counterArr; counterArr--) {
         if (lowline[y - 1] == arr[i]) {
             arr.splice(i,1);
             break;
         }
         else i++;
         }
                if (arr.length == 0) {fullHouse = 1;}

}
     } //проверка 1 слова на все буквы алфавита


if (wordLength >= 33) {
for (let y = wordLength + 2; y <= lowline.lenght && result > 0; y++) {
       for (let i = 0; i <= counterArr; counterArr--) {
         console.log(lowline[y-1],arr[i]);
         if (lowline[y - 1] == arr[i]) {
             arr.splice(i,1);
             break;
         }
         else i++;
         }
                if (arr.length == 0 && fullHouse == 1) {return result = 0;}

}
     } //проверка 2 слова на все буквы алфавита, если в первом и втором словах все буквы есть - result 0


    for (let i = 1; i <= wordLength && result > 0; i++) {
        for (let compare = i + 1; compare <= wordLength; compare++) {
            if (lowline[i - 1] == lowline[compare - 1]) {
                if (lowline[i + wordLength] == lowline[compare + wordLength]) {
                    result = 1;
                } else {
                    return result = 0; //ищу одинаковые символы в первой строке, сравниваю на этих же индексах буквы во втором слове
                    
                }
            }
        }
    }
    console.log(String(result));  
    rl.close();
    return;
}).on("close", () => process.exit(0));


I'm scratching my head for the second day, there are 2 attempts left to check the validator, I ask for the help of experienced people

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-09-26
@Tush

The answer is "possible" if
1) Each character of the first line corresponds to the same character of the second line
2) If there are replacements in the form of a cycle (like a-> b, b-> c, c-> a), then at least one is needed a free letter (one that is not on the right in the replacement).
It is solved like this:
1) Check that the strings are equal. This is a special case
2) Check that the length is the same?
3) Start map symbol-> symbol, go through the lines in parallel and write map[line1[i]] = line2[i], if it is empty. Otherwise, check that the same thing is already written there.
4) Check that there are less than 33 different characters in the second line. You can use the set, which is filled in the same loop as in step 3.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question