Thursday, 5 October 2023

EM-Tirupati Codeathon Series #01

[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".
 
[FunctionDescription]
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
 
[Explanation of Solution]
The program takes a list of usernames as input. It checks if each username can be modified by rearranging its letters to be in ascending order. If the modification is possible, it labels the username as "YES"; otherwise, it labels it as "NO." The program then prints these "YES" or "NO" labels for each username. The goal is to determine if a username can be changed to come before itself in dictionary order.
 
Github Repo for Code:  https://tinyurl.com/4wervn4f

[Solution (Java Code)]
/*
 * Core Java Problem Solving - Codeathon.
 * A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
 *
 */
package codeathon;
import java.util.Scanner;
public class Codeathon01_Kishore
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();                  // To store number of user names
        String usernames[] = new String[n];    // To store an array of user names
        String results[];                     // To store YES / NO  values
 
        for (int i = 0; i < n; i++)                    // Storing n number of user names
            usernames[i] = sc.next();
 
        //calling method possibleChanges with usernames
        results = UsernamesDictionaryLogic.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 UsernamesDictionaryLogic
{
    // 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 dummy = usernames[i].toLowerCase();
            // checking for possible change in the user name by comparing the characters
            for(int j=1; j<dummy.length(); j++)
            {
                if ((dummy.charAt(j-1))> (dummy.charAt(j)))
                {
                    check++;
                }
            }
            if (check > 0)
                results[i]="YES";
            else
                results[i]="NO";
        }
        return results;                                             
    }
}

No comments:

Post a Comment