We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is not the prettiest of solutions and I am still new to coding in general. I just wanted to share my results from this test with the knowledge I have right now. This solution while is not the prettiest passes all the test cases, I added comments for anyone who is learning just as myself. I encourage anyone learning to not just use random code you find in the comments if you don't completely understand and work through the solution with what you know. In this case I worked backwards getting what needed to print, printed. Then I worked on solve each of the problems needed one by one, removed redundant prints, and added comments. I worked on each block as I passed each scenario grouping.
Revised Removed redundant code.
public class Solution {
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. */
//Create a string with a cap letter for both words that will be used only for system out.
String word1 = A.substring( 0, 1).toUpperCase() + A.substring(1);
String word2 = B.substring( 0, 1).toUpperCase() + B.substring(1);
//Prints the total lengh of both words
System.out.println(word1.length() + word2.length());
//iterator variable
int i = 0;
//While loop that will iterate for as long as the length of the first word
while (i < A.length()) {
//If statement to compare for the first letter of both words to see if they have a common letter to start.
if (A.charAt(i) == B.charAt(i)) {
i++;
//If statement to check if both words match in completeness from all the letters, to the length.
if (A.charAt(A.length()-1) == B.charAt(B.length()-1)) {
System.out.println("No");
break;
}
//else statement to capture words that match up to a certain point but where the first word is shorter then the second word.
else {
System.out.println("No");
break;
}
}
//If statement to capture when a letter does not match.
else if (A.charAt(i) != B.charAt(i)) {
//if statement to compare the letters and determine is A is greater then B.
if (A.charAt(i) > B.charAt(i)) {
System.out.println("Yes");
break;
}
//Else to print out if A is not greater then be.
else {
System.out.println("No");
break;
}
}
}
//Statement to print our regardless of the outcome.
System.out.println(word1 + " " + word2);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Strings Introduction
You are viewing a single comment's thread. Return to all comments →
This is not the prettiest of solutions and I am still new to coding in general. I just wanted to share my results from this test with the knowledge I have right now. This solution while is not the prettiest passes all the test cases, I added comments for anyone who is learning just as myself. I encourage anyone learning to not just use random code you find in the comments if you don't completely understand and work through the solution with what you know. In this case I worked backwards getting what needed to print, printed. Then I worked on solve each of the problems needed one by one, removed redundant prints, and added comments. I worked on each block as I passed each scenario grouping. Revised Removed redundant code.
public class Solution {
}