Answer the question
In order to leave comments, you need to log in
The algorithm is not accepted by the validator, what is wrong with it?
Good afternoon, I have
a question about a test task. I can't figure out what the validator could have attached itself to
. Task condition
Time limit, s 1
Memory limit, MB 64
Total number of attempts to send 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));
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
//сделал возвращаемый int а не boolean тк в задании указано что требуется именно 1 или 0
public static void main(String[] args) {
// long m = System.currentTimeMillis();
// long usedBytes = Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
// System.out.println(check("привет прикол"));
// System.out.println(check("абвгдеёжзийклмнопрстуфхцчшщъыьэюя яюэьыъщшчцхфутсрпонмлкйизжёедгвба"));
// System.out.println((double) (System.currentTimeMillis() - m));
// System.out.println(usedBytes/1048576 + "m");
// System.out.println(check(args[0]));
try(Scanner scanner = new Scanner(System.in)) {
String str = "";
str = scanner.nextLine();
System.out.println(check(str));
}
}
public static int check(String str){
if (str == null) {
return 0;
} else if (!(str.matches("^[а-яё]+\\s[а-яё]+$"))) {
return 0;
}
String[] strings = str.trim().split(" ");
String first = strings[0];
String second = strings[1];
if (first.length() != second.length()) {
return 0;
} else if (first.equals(second)) {
return 1;
// } else if (!(first.matches("^[а-я]+$")&&second.matches("^[а-я]+$"))) {
// return 0;
} else {
Map<Character, Character> map1 = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
char[] firstChars = first.toCharArray();
char[] secondChars = second.toCharArray();
//-->
for (int i = 0; i < firstChars.length; i++) {
if (map1.containsKey(firstChars[i])) {
if (secondChars[i]!=map1.get(firstChars[i]))
return 0;
} else {
map1.put(firstChars[i], secondChars[i]);
}
}
//<--
for (int j = 0; j < secondChars.length; j++) {
if (map2.containsKey(secondChars[j])) {
if (firstChars[j] != map2.get(secondChars[j]))
return 0;
} else {
map2.put(secondChars[j], firstChars[j]);
}
}
if (map1.size() == 33&&map2.size()==33) {
return 0;
}
}
return 1;
}
}
Answer the question
In order to leave comments, you need to log in
'abvgdeejklmnoprostufxchshchjyeyuya' => 'bavgdejjjklmnoprstufxchshchjyeyuya'
The result should be 0, you have 1.
The trick is that if there is a cycle of substitutions (a => b => a), then there must be at least one free letter in order to break this cycle . In this case, the cycle can be from an arbitrary number of replacements (a => b => c => d => a). Keep in mind that the letter may be released during previous replacements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question