Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
Write a Java program that displays all 4-character strings in lexicographic order (alsocalled dictionary order) on the screen. It must display a string of 4 characters per line.Only the characters a,
Write a Java program that displays all 4-character strings in lexicographic order (alsocalled dictionary order) on the screen. It must display a string of 4 characters per line.Only the characters a, b, c, d are allowed within character strings, but you can reuse thesame character more than once within the same string. Within the same string, thecharacter "b" must always be immediately followed by the character "a". The same stringcannot contain both the character "d" and the character "a". Your program should end withthe number of character strings displayed on the screen. Your program receives nothingas input. Your program code must not include pre-calculated strings: it must generatethem at runtime. Your program should not contain more than 200 short lines of code.
- @
- ANSWER
public class Lexical {
public static void main(String[] args) {
displayAllStrings("abcd");
}
public static void displayAllStrings(String s ) {
char[] ArrayCharacter = new char[s.length()];
Helper(s , ArrayCharacter , 0, s.length()-1);
}
public static void Helper(String word, char[] Arr, int first , int end )
{
for (int i = 0; i < word.length(); i++)
{
Arr[first] = word.charAt(i);
if (first == end )
{
String string = new String(Arr);
if( !(string.charAt(3)=='b' || string.contains("a") && string.contains("d")) && CheckRules( string ))
System.out.println(string);
}
else
Helper(word, Arr, first + 1 , end );
}
}
public static boolean CheckRules(String word){
for(int i=0;i<word.length()-1;i++)
{
if(word.charAt(i)=='b' && word.charAt(i+1)!='a')
return false ;
}
return true ;
}
}// class