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
|
1766 Discussions
|
Please Login in order to post a comment
I create different solution, but passed tests: public static String getSmallestAndLargest(String s, int k) {
public static String getSmallestAndLargest(String s, int k) { String smallest = s.substring(0, k); String largest = s.substring(0, k);
}
I propose an optimized guided search with sub-strings' starting points' preselection. After getting the smallest and the largest characters in the input string, I use them as guides that lead the iteration through the preselected category of sub-strings by repeating evaluation of the expression
pos=str.lastIndexOf(cat, pos - 1), whereposbecomes a "guided reverse itarator" through the category.To get rid of the error-prone repetitions, I abstracted the search code with comparator passed as argument, together with the category mark to guide the serch in the given order.
Implementation:
import java.util.Scanner;
public class Solution {
}
public static String getSmallestAndLargest(String s, int k) { String smallest = ""; String largest = "";
}
public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); int k = scan.nextInt(); scan.close();
}