-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstring-compare.java
More file actions
62 lines (53 loc) · 2.15 KB
/
string-compare.java
File metadata and controls
62 lines (53 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Easy
// https://www.hackerrank.com/challenges/java-string-compare/problem
import java.util.Scanner;
public class Solution {
public static boolean compare(String a, String b, int c) {
boolean diff = false;
for (int i = 0; i < a.length(); i++) {
//System.out.printf("compare %d %d: %s %s\n", c, i, a, b);
if (c < 0) {
if (a.charAt(i) < b.charAt(i)) return true;
if (a.charAt(i) > b.charAt(i)) return false;
} else if (c > 0) {
if (a.charAt(i) > b.charAt(i)) return true;
if (a.charAt(i) < b.charAt(i)) return false;
}
}
return diff;
}
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
int size = s.length() - k + 1;
String words[] = new String[size];
for (int c = 0; c < size; c++) {
words[c] = s.substring(c, c + k);
}
smallest = largest = words[0];
for (int w = 0; w < words.length; w++) {
//System.out.printf("%d %s: %s %s\n", w, words[w], smallest, largest);
/*
if (words[w].charAt(0) < smallest.charAt(0))
smallest = words[w];
if (words[w].charAt(0) > largest.charAt(0))
largest = words[w];
*/
// Comparing just the first letter failed for Testcase(s) 2,4
// Write function to compare each letter until unmatched
if (compare(words[w], smallest, -1)) smallest = words[w];
if (compare(words[w], largest, 1)) largest = words[w];
}
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();
System.out.println(getSmallestAndLargest(s, k));
}
}