Here is a Java practice problem taken from HackerRank that tests your knowledge of strings. The problem involves arranging strings in lexicographical order and capitalizing the first letter of each word. It's a great exercise for revision and practice. Here's the problem description and a sample solution:
Problem statement:
Given two strings of lowercase English letters, A and B, perform the following operation:
- Sum of length of A and B.
- Determine if A is lexicographically larger than B(i.e.: B comes after A in dictionary?).
- Capitalize the first letter of A and B and print them on a single line, separated by a space.
Input Format
The first line contains a string A. The second line contains another string B. The strings are comprised of only lowercase English letters.
Output Format
There are three lines of output:
For the first line, sum the lengths of A and B.
For the second line, write Yes if is A lexicographically greater than B otherwise print No instead.
For the third line, capitalize the first letter in both A and B and print them on a single line, separated by a space.
Sample Input
hello
java
Sample Input
9
No
Hello Java
Explanation
String A is “hello” and B is “java”. A has a length of 5, and B has a length of 4; the sum of their lengths is 9.
When sorted alphabetically/lexicographically, “hello” precedes “java”; therefore, A is not greater than B and the answer is No. When you capitalize the first letter of both A and B and then print them separated by a space, you get “Hello Java”.
// My solution:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String A = scanner.nextLine();
String B = scanner.nextLine();
scanner.close();
String lCaseA= A.toLowerCase();
String lCaseB=B.toLowerCase();
System.out.println(lCaseA.length()+lCaseB.length());
String flag="Yes";
int min=lCaseB.length();
if(lCaseA.length()<=lCaseB.length()) min=lCaseA.length();
//System.out.println(max);
int i=0;
while(i<min)
{
if((int)lCaseA.charAt(i)<(int)lCaseB.charAt(i))
{
flag="No";
break;
}
else if((int)lCaseA.charAt(i)==(int)lCaseB.charAt(i))
{
i++;
flag="No";
continue;
}
else
{
break;
}
}
//System.out.println(lCaseA);
//System.out.println(lCaseB);
System.out.println(flag);
String ucase=" ";
Scanner scaner=new Scanner(lCaseA);
while(scaner.hasNext())
{
String word=scaner.next();
ucase+=Character.toUpperCase(word.charAt(0))+word.substring(1)+" ";
}
System.out.print(ucase.trim()+" ");
scaner.close();
String ucase1=" ";
Scanner scaner1=new Scanner(lCaseB);
while(scaner1.hasNext())
{
String word1=scaner1.next();
ucase1+=Character.toUpperCase(word1.charAt(0))+word1.substring(1)+" ";
}
System.out.println(ucase1.trim());
scaner1.close();
}
}
// program ends
While the provided solution uses two separate while loops for simplicity,
it is worth mentioning that a dedicated function can be created to encapsulate
the logic of capitalizing the first letter of a word. This approach can make the
code more modular and readable. (For the sake of better understanding I showed
with two separate loops)
By practicing this problem, you will gain a deeper understanding of string
manipulation and improve your ability to solve similar challenges efficiently.
Mastery of these concepts will prove valuable in real-world scenarios where
string handling plays a crucial role.
Remember to consider edge cases, such as handling empty strings or strings with
leading/trailing spaces(trim()). Understanding these nuances will enhance the robustness
of your solution and make it applicable in various scenarios.
Conclusion:
This Java practice problem focusing on arranging strings in lexicographical
order and capitalizing the first letter of each word provides an excellent
opportunity to revise and strengthen your skills in string manipulation.
By understanding and implementing the solution, you will enhance your ability to
work with strings effectively and tackle similar challenges with confidence.
Practice this problem and continue exploring more string-related problems to
expand your knowledge and become proficient in different programming logic.