[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
[Explanation Solution]:
IN this Question have Problem Solving Abilities. The Core Point to Handle is that For Each Combination of 2 Alphabets that Exists in the Username String .Finding which alphabetic is greater than both of them through (ASCII)values.Whether it is follow this condition print "YES " or else "NO".
Github Repo for Code:https://github.com/em-tpt-kvijay/emt_codeathon_sep_2023_solutions/blob/master/src/codeathon/Codeathon01_vijay.java
[Solution (Java Code)]
package codeathon;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Codeathon01_vijay{
public static List<String> findModifyUserName(String[] customers)
{
List<String> result = new
ArrayList<>();
for (String str : customers) {
String lowerCaseString =
str.toLowerCase();
int stringLen
=lowerCaseString.length();
boolean flag=false;
for(int i=0;i < stringLen;i++){
int a=lowerCaseString.charAt(i);
for(int j=i+1;j<
stringLen;j++){
int
b=lowerCaseString.charAt(j);
if(a>b){
flag=true;
}
}
}
if(flag==true){
result.add("YES");
}
else {
result.add("NO");
}
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter the
number of userNames to add: ");
int numUserNames = scanner.nextInt();
scanner.nextLine();
String[] user = new
String[numUserNames];
System.out.println("Enter the
userName (one on each line):");
for (int i = 0; i < numUserNames;
i++) {
user[i] = scanner.nextLine();
}
System.out.println("OUTPUT:");
List<String> modifyUserName =
findModifyUserName(user);
for (String userName :
modifyUserName) {
System.out.println(userName);
}
}
}
Thank you,
k.vijay(Intern)
vijay.keradhi@eminds.ai
Enterprise Minds.
No comments:
Post a Comment