개요[ | ]
- HR자바 Java Substring Comparisons
Java
Copy
import java.util.Scanner;
public class Solution {
Java
Copy
public static String getSmallestAndLargest(String s, int k) {
String smallest = s.substring(0,k);
String largest = s.substring(0,k);
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
String ss;
for(int i=1; i<s.length()-k+1; i++) {
ss = s.substring(i,i+k);
if( ss.compareTo(smallest)<0 ) smallest=ss;
if( ss.compareTo(largest)>0 ) largest=ss;
}
return smallest + "\n" + largest;
}
Java
Copy
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int k = scan.nextInt();
scan.close();
System.out.println(getSmallestAndLargest(s, k));
}
}
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.