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.
- Prepare
- Java
- Strings
- Java Substring Comparisons
- Discussions
Java Substring Comparisons
Java Substring Comparisons
Sort by
recency
|
1719 Discussions
|
Please Login in order to post a comment
does my code have any problem, it always failed to test case 3, however when i try the input of test case 3 as custom input, the output is correct.
Perfectly working, Logic is just that you need to keep checking all the substrings untill compateTo() function filters all the substrings lexographically.
}
import java.util.Scanner;
public class Solution {
}
works perfectly
public class solution{ public static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0,k); String largest = s.substring(0,k); int i; for(i = 1 ; i<=s.length() - k ; i++){ String comp = s.substring(i,i+k); if(comp.compareTo(smallest)<0){ smallest = comp; } if(comp.compareTo(largest)>0){ largest = comp; } return smallest +"\n"+largest; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); int k = scan.nextInt(); scan.close();
}
import java.util.Scanner;
public class Solution {
}