Предположат длину элемента в таблице 1 или 2. Таблица : {ч, Fe, Na, O} входной строка: NAFEO Выхода: истиннаяДля проверки входной строки присутствует в пользовательской таблице строк
Таблица: {AB, BC} строка ввода: абв Выход: ложные
Пожалуйста посоветуйте мой код ниже будет охватывать все случаи и это самое лучшее решение? Или я что-то упускаю, какие-то альтернативные решения?
import java.util.*;
public class CustomTable {
Set<String> table = new HashSet<String>();
public CustomTable(){
// add your elements here for more test cases
table.add("oh");
table.add("he");
}
public int checkTable(String prev, String curr, String next) {
System.out.print(prev+":"+curr+":"+next);
System.out.println();
if (prev!=null) if (table.contains(prev)) return -1;
if (table.contains(curr)) return 0;
if (table.contains(next)) return 1;
return 2;
}
// ohhe.
public static void main(String args[]) {
CustomTable obj = new CustomTable();
String inputStr = "ohheo"; //Tested ohe,ohhe,ohohe
int result = 0;
String curr, prev, next;
for (int i = 0; i < inputStr.length(); i++) {
// if prev element is found
if (result==-1){
prev = null;
}
else {
if (i > 0) {
prev = inputStr.substring(i - 1, i + 1);
} else {
prev = inputStr.substring(i, i + 1);
}
}
curr = inputStr.substring(i,i+1);
if (i < inputStr.length()-1) {
next = inputStr.substring(i, i+2);
} else {
next = inputStr.substring(i, i+1);
}
result = obj.checkTable(prev, curr, next);
if (result==2) {
System.out.print("false");
return;
}
}
System.out.print("true");
}
}
Под кодом ниже? Вы хотите, чтобы мы написали код? – Haris
не в состоянии вставить код, превышающий размер. – pressf1