Answer the question
In order to leave comments, you need to log in
They gave me a test task, I solved it, but why is it wrong?
The input is 2 lines. 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.
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 funny
Output: 1
Transforms (no need to output):
c ⇒ k (priket)
e ⇒ o (priket)
t ⇒ l (joke)
import java.io.IOException;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("");
String str = "";
str = scanner.nextLine();
if (!str.matches("[а-я ]+")) {
System.out.println(0);
return;
}
str = str.trim().replaceAll(" +", " ").trim();
System.out.println(str);
String[] subOne = str.split(" ");
String string = Stream.of(subOne[0].split(""))
.map(x -> {
if (x.equals("в")) return "к";
if (x.equals("е")) return "о";
if (x.equals("т")) return "л";
return x;
})
.collect(Collectors.joining());
if (string.equals(subOne[1])) System.out.println(1);
else System.out.println(0);
}
}
Answer the question
In order to leave comments, you need to log in
Because your code only works for sample input. The task assumes that any strings can be entered.
In my opinion, there is quite a clear algorithm here.
1) You will never turn one line into another if you have interdependent letters in the original word (that is, the same), but which are different in the second word.
No matter how you change them in the original word, they will still remain the same.
2) If the length of the words is different.
Here on this basis and throw the algorithm
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question