Java Strings Introduction Solution
Problem Overview
Original Problem: Java Strings Introduction | Hackerrank
Difficulty: Easy
Category: Strings
Description: We are given two lowercase strings. Our task is to perform three actions. Firstly, we need to print the total number of characters in both strings. Secondly, we need to determine if the first string would come after the second in the dictionary (lexicographic order). Lastly, we need to captilize the first letter of each string and print them.
Understanding the Problem
This problem is more easily approached by breaking it down into the three steps, keeping in mind the two words made of lowercase letters. For the first step, we will need to calculate the length of each word and add them together. For the second step, we will need to compare the words alphabetically. For the third step, we will need to convert just the first letter of each word to an uppercase character.
Input:
Two lines of text, each being one lowercase string
Output:
Three lines:
The sum of the lengths of both strings
‘Yes’ if the first string comes after in the dictionary, ‘No’ otherwise
The two strings separated by a space, with each string beginning with a capital letter
Initial Reasoning
The original problem gives us template code to read in the two strings. From here, we will perform each step in sequence to make it easier to follow. For the first step, we can use the String.length() method to get the number of characters in each word. For determining dictionary order, we can use the String.compareTo() method — this returns a positive number if the first string comes after the second. Finally, we can use substring() and toUpperCase() to extract and capitalize the first letter of each string.
Pseudocode
// Main function:
Read the first string
Read the second string
Calculate and print the sum of their lengths
If the first string comes after the second string in dictionary order
Print 'Yes'
Else
Print 'No'
Capitalize the first letter of each string and print them separated by a space
Solution Code
// Note this is one of many possible solutions
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
// Read in the strings
Scanner scanner = new Scanner(System.in);
String firstString= scanner.next();
String secondString = scanner.next();
scanner.close();
// Calculating and printing the sum of the length of the strings for the first line of output
System.out.println(firstString.length() +secondString.length());
// Determining lexicographical order and printing the required second line of output
// If the first string comes after the second string in the dictionary
if (firstString.compareTo(secondString) > 0) {
System.out.println("Yes");
} else {
System.out.println("No");
}
// Capitalize the first letter of each string by taking only the first letter, converting it to upper case, then
// concatenating it with all the letters in the string after the first letter
String capitalizedFirstString = firstString.substring(0, 1).toUpperCase() + firstString.substring(1);
String capitalizedSecondString = secondString.substring(0, 1).toUpperCase() + secondString.substring(1);
// Printing the formatted strings for the third line of output
System.out.println(capitalizedFirstString + " " + capitalizedSecondString );
}
}
Commenting on Alterations
While we have done our best to maintain similarity to the template code provided in the original problem, the following changes have been made to follow best practices:
Renamed variables:
Using more descriptive names improves the clarity of the code and makes it easier for anyone reading the code to understand what each variable represents.