-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava Substring Comparisons.java
47 lines (36 loc) · 1.04 KB
/
Java Substring Comparisons.java
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
import java.util.Scanner;
public class Solution {
public static String getSmallestAndLargest(String s, int k) {
String smallest = "z";
String largest = "A";
int n = s.length();
int min_value = 'z';
int max_value = 'A';
int min,max;
min= max = 0;
for (int i = 0; i <= s.length() - k; i++) {
String str = s.substring(i, i+k);
if (smallest == null || str.compareTo(smallest) < 0) {
smallest = str;
}
if (largest == null || str.compareTo(largest) > 0){
largest = str;
}
}
/*for(int i=0;i<(n-k+1);i++)
{
int t = s.charAt(i);
if(t>max_value)
{
max= i;
max_value =t;
}
if(t<min_value)
{
min =i;
min_value = t;
}
}*/
//System.out.print
return smallest + "\n" + largest;
}