[Question]
Algorithms/Data Structures — [Problem Solving]
There is a Specific Need for Changes in a List of Usernames.
In a given List of Usernames — For Each Username — If the Username can be Modified
and Moved Ahead in a Dictionary. The Allowed Modification is that Alphabets can
change Positions in the Given Username.
Example
usernames[] = {“Aab”, “Cat”}
“Aab” cannot be changed to another unique string matching
the above rule — Hence, It can Never Find a Place Ahead in the Dictionary.
Hence, Output will be “NO”. “Cat” can be Changed to “Act”, “Atc”, “Tca”, “Tac”,
“Cta” and Definitely “Act” will Find a Place Before “Cat” in the Dictionary.
Hence, Output will be “YES”.
[Function Description]
Complete the function possible Changes in the Editor Below.
Possible Changes has the following parameters:
String usernames[n]: An Array of User Names.
Returns String[n]: An Array with “YES” or “NO” Based on
Feasibility
(Actual Question Says String Array, But Signature is List of
Strings)
Constraints
• [No Special Constraints Exist, But Cannot Recall Exactly]
[Sample Input]
“The First Line Contains an Integer, n, the Number of
Elements in Usernames.”,
“Each Line of the n Subsequent Lines (where 0 < i < n)
contains a String usernames[i].”
[Sample Case 0 — Sample Input For Custom Testing]
8
Aab
Cat
Pqrs
Buba
Bapg
Sungi
Lapg
Acba
[Sample Output] (Each Should Be on a Separate Line)
NO YES NO YES YES YES YES YES
Code Explaination:
UsernameValidation is a utility class that encapsulates methods related to username validation and result display.
- The possibleChanges method takes a username as input and checks if it can be modified. It compares each character with the characters that follow it and returns true if any character is followed by a smaller character, indicating that the username can be modified.
- The displayResults method takes an array of boolean results and displays them as "YES" if the result is true or "NO" if it's false.
- The Question01_Venkata class is the main class that contains the main method and serves as the entry point for the program.
- An instance of the UsernameValidation class called validate is created to utilize its validation and display methods.
- Two Scanner objects, scanner and scanner1, are created to read user input.
- The user is prompted to input the number of usernames using scanner.nextInt().
- Another prompt is displayed, instructing the user to enter string values (usernames).
- An array called usernames is created to store the entered usernames.
- A boolean array named results is created to store the validation results for each username.
- The possibleChanges method from the validate object is called to validate the username and store the result in the results array.
- After all usernames are validated, the displayResults method from the validate object is called to display the results.
- Results are shown on the console as "YES" if the validation result is true or "NO" if it's false in an Array.
Source code (Java) :
import java.util.Scanner;
// This class is for username validation and result display
class UsernameValidation {
// Method to check if the username can be modified
public static Boolean possibleChanges(String username) {
for (int i = 0; i < username.length(); i++) {
char currentChar = username.charAt(i);
for (int j = i + 1; j < username.length(); j++) {
char nextChar = username.charAt(j);
if (nextChar < currentChar) {
return true;
}
}
}
return false;
}
// Method to display the results
public static void displayResults(boolean[] results) {
System.out.println("Output:");
for (boolean result : results) {
System.out.println(result ? "YES" : "NO");
}
}
}
public class Question01_Venkata {
public static void main(String[] args) {
// Creating an instance of UsernameValidation
UsernameValidation validate = new UsernameValidation();
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the Size:");
int size = scanner.nextInt();
Scanner scanner1 = new Scanner(System.in);
System.out.println("Please enter the Usernames:");
// Create an array to store usernames
String[] usernames = new String[size];
for (int i = 0; i < size; i++) {
// Read each username, convert it to lowercase, and store it in the array
usernames[i] = scanner1.next().toLowerCase();
}
// Create an array to store the results
boolean[] results = new boolean[size];
// Loop through each username and validate it using the UsernameValidation object
for (int i = 0; i < size; i++) {
results[i] = validate.possibleChanges(usernames[i]);
}
// Display the results using the displayResults method
validate.displayResults(results);
}
}
Thank you
Venkata kishore T(Intern)
Shield Warriors,
Data Shield Team,
Enterprise Minds.
No comments:
Post a Comment