Created
July 26, 2018 19:14
-
-
Save AbdullahMagat/dd823ea3a4041626d1369875e610bda1 to your computer and use it in GitHub Desktop.
Hackerrank Java Substring Comparisons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Solution { | |
public static String getSmallestAndLargest(String s, int k) { | |
String smallest = ""; | |
String largest = ""; | |
smallest = s.substring(0,k); | |
largest = s.substring(0,k); | |
// "Compare to" method doesn't turn just the equel case it also turns a value. | |
for(int i=0; i<=s.length()-k; i++ ){ | |
String str = s.substring(i,k+i); | |
if (smallest.compareTo(str)>0){ | |
smallest = str; | |
} | |
if(largest.compareTo(str)<0){ | |
largest=str; | |
} | |
} | |
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)); | |
} | |
} |
My solution:
package org.redbull4.helloworld;
import java.util.*;
public class Hello {
static Scanner sc = new Scanner(System.in);
public static String getSmallestAndLargest(String s, int k) {
String _String = "", smallest = s.substring(0, k),
largest = s.substring(0, k), output = "";
for (int i = 0; i < s.length(); i++) {
if (i + k > s.length())
break;
_String += s.substring(i, i + k);
if (smallest.compareTo(_String) > 0)
smallest = _String;
if (largest.compareTo(_String) < 0)
largest = _String;
_String = "";
}
output = smallest + '\n' + largest;
return output;
}
public static void main(String[] args) {
String s = sc.nextLine();
int k = sc.nextInt();
System.out.println(getSmallestAndLargest(s, k));
}
}
Should be
String smallest = s.substring(0,k+1);
String largest = s.substring(s.length()-k);
Do anyone have list of all test cases ?
String Comparision Java - HackerRank
we save one iteration by using i=1 instead of i=0;
What about this?
public static String getSmallestAndLargest(String s, int k) {
List<String> subs = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (k > s.length()) {
break;
}
subs.add( s.substring(i, k));
k++;
}
subs = subs.stream().sorted().collect(Collectors.toList());
return subs.get(0) + "\n" + subs.get(subs.size() - 1);
}
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
ArrayList<String> st = new ArrayList<>();
int s_len = s.length();
for (int i =0; i<=s_len-k; i++){
String sk = s.substring(i,i+k);
st.add(sk);
}
smallest = Collections.min(st);
largest = Collections.max(st);
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));
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import java.util.Scanner;
public class Solution {
}