Java Strings - 1 Hackerrank
This exercise is to test your understanding of Java Strings. A sample String declaration:
String myString = "Hello World!"
The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.
Given two strings of lowercase English letters,
and, perform the following operations:
- Sum the lengths of
- and print them on a single line, separated by a space.
Input Format
The first line contains a string
. 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
For the second line, write
Yes
if is lexicographically greater than otherwise print No
instead. For the third line, capitalize the first letter in both and
and print them on a single line, separated by a space.
Sample Input 0
hello
java
Sample Output 0
9
No
Hello Java
Explanation 0
String
is "java".
has a length of , and has a length of ; the sum of their lengths is .
When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than
and the answer is No
.
When you capitalize the first letter of both
and and then print them separated by a space, you get "Hello Java".
import java.io.*;
import java.util.*;
public class Solution {
public static String capitalize(String str) {
if(str == null || str.isEmpty()) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
/* Enter your code here. Print output to STDOUT. */
int sum = A.length() +B.length();
String concat = capitalize(A)+" "+capitalize(B);
List<String> lst = new LinkedList<String>();
lst.add(A);
lst.add(B);
Collections.sort(lst);
System.out.println(sum);
if (lst.get(0) == A){
System.out.println("No");
}else{
System.out.println("Yes");
}
System.out.println(concat);
}
}
Comments
Post a Comment