Monday, October 9, 2023

Codeathon 01_Haritha

[Question]


 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


Program:

package src;

import java.util.Scanner;

public class Codeathon01_Haritha

{
 
public static void main(String args[])
 
{

Scanner sc = new Scanner(System.in);

 int n = sc.nextInt();                  // It stores the User names

String usernames[] = new String[n];

String results[];                     // To store YES / NO  values
 
for (int i = 0; i < n; i++)
 
usernames[i] = sc.next();            
//calling method possible Changes with usernames

results =DictionarySeries.possibleChanges(usernames); 
 //displaying the results of possible changes

YES / NO for user names

for (int i = 0; i < n; i++)

 System.out.println(results[i] + "\t");

}

}

class DictionarySeries

{
 
// method to know possible changes in each user name
 
public static String[] possibleChanges(String usernames[])

 {
 
int n = usernames.length;

String results[] = new String[n];

int check;
 
// starting from first user name, checking each user name for possible changes

for(int i=0;i<n;i++)
 
  {
 
check=0;
 
String duplicate = usernames[i].toLowerCase();
 
// checking for possible change
 
for(int j=1; j<duplicate.length(); j++)
 
{
 
if ((duplicate.charAt(j-1))> (duplicate.charAt(j)))
 
{
 
check++;

}

}
 
if (check > 0)

results[i]="YES";

else

results[i]="NO";
 
}
 
return results;

}

}

[Explanation of Solution]

1. The code begins by defining a class called "Codeathon01_Haritha," which appears to be a program for processing and checking a series of usernames.

2. In the main method of "Codeathon01_Haritha," the program starts by creating a Scanner object to read input from the user. It then takes an integer 'n' as input, which represents the number of usernames the user wants to enter. An array called "usernames" is declared to store these usernames, and another array called "results" is declared to store whether each username has possible changes (either "YES" or "NO").

3.Next, a for loop is used to read 'n' usernames from the user and store them in the "usernames" array.

4. After collecting the usernames, the program calls a method named "possibleChanges" from the "DictionarySeries" class to determine if there are any possible changes needed for each username.

5. In the "DictionarySeries" class, there is a method called "possibleChanges" that takes an array of usernames as input. Inside this method, it iterates through each username and checks if there are any characters in the username that are not in ascending alphabetical order. If such characters are found, the "check" variable is incremented, indicating that there are possible changes needed for that username.

6. After processing all characters in a username, if the "check" variable is greater than 0, it means there are possible changes required, and the corresponding result in the "results" array is set to "YES." Otherwise, if "check" is 0, it means no changes are needed, and the result is set to "NO".

7. Finally, the program prints the results for each username, indicating whether each username needs possible changes ("YES") or not ("NO")




Haritha .P(Intern),
Guard Ninjas,
Data Shield Team,
Enterprise Minds.    



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home