Skip to content

Commit 5a38038

Browse files
committed
Solve Hackerrank problem
Problem - https://www.hackerrank.com/challenges Implement ParameterizedTest
1 parent 00ed376 commit 5a38038

File tree

3 files changed

+90
-43
lines changed

3 files changed

+90
-43
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package hackerrank.Java;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Scanner;
6+
7+
public class CompareSubstrings {
8+
/**
9+
* Given String s, find the lexicographically
10+
* smallest and largest substrings of length k.
11+
*
12+
* Parameters:
13+
* string s: a string
14+
* int k: the length of the substrings to find
15+
*
16+
* Returns
17+
* string: the string ' + "\n" + ' where and are the two substrings
18+
*/
19+
20+
public String getSmallestAndLargest(String s, int k) {
21+
String smallest = "";
22+
String largest = "";
23+
24+
ArrayList sortedSubString = getSortedSubString(s, k);
25+
smallest = sortedSubString.get(0).toString();
26+
largest = sortedSubString.get(
27+
sortedSubString.size() - 1).toString();
28+
29+
return smallest+ " " + largest;
30+
}
31+
32+
private ArrayList getSortedSubString(String s, int k) {
33+
String smallest = "", largest = "";
34+
35+
int i, j;
36+
int stringLength = s.length();
37+
ArrayList<String> subStringList
38+
= new ArrayList<String>();
39+
40+
for (i = 0; i < s.length()-k+1 ; i++) {
41+
subStringList.add(s.substring(i, k+i));
42+
}
43+
44+
Collections.sort(subStringList);
45+
return subStringList;
46+
}
47+
48+
public static void main(String[] args) {
49+
Scanner scan = new Scanner(System.in);
50+
String s = scan.next();
51+
int k = scan.nextInt();
52+
scan.close();
53+
54+
CompareSubstrings compareSubstrings =
55+
new CompareSubstrings();
56+
57+
58+
System.out.println(
59+
compareSubstrings.getSmallestAndLargest(
60+
s, k));
61+
}
62+
}
63+
64+
65+
/**
66+
* Problem
67+
* https://www.hackerrank.com/challenges/java-string-compare
68+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package hackerranktest.java;
2+
3+
import hackerrank.Java.CompareSubstrings;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
9+
public class CompareSubstringsTest {
10+
@ParameterizedTest
11+
@CsvSource({
12+
"welcometojava, 3, ava wel",
13+
"ASDFHDSFHsdlfhsdlfLDFHSDLFHsdlfhsdlhkfsdlfLHDFLSDKFHsdfhsdlkfhsdlfhsLFDLSFHSDLFHsdkfhsdkfhsdkfhsdfhsdfjeaDFHSDLFHDFlajfsdlfhsdlfhDSLFHSDLFHdlfhs, 30, ASDFHDSFHsdlfhsdlfLDFHSDLFHsdl sdlkfhsdlfhsLFDLSFHSDLFHsdkfhs"
14+
})
15+
void toUpperCase_ShouldGenerateTheExpectedUppercaseValue(
16+
String s, int k, String expected) {
17+
18+
CompareSubstrings compareSubstrings = new CompareSubstrings();
19+
String actual = compareSubstrings.getSmallestAndLargest(s, k);
20+
Assertions.assertEquals(expected, actual);
21+
}
22+
}

src/test/java/whiteboardtest/WhiteboardTest.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,9 @@ public void shouldReturnTrueForOddNumbers(int number) {
2121
Assertions.assertTrue(isOdd(number));
2222
}
2323

24-
/**
25-
* Given String s, find the lexicographically
26-
* smallest and largest substrings of length k.
27-
*
28-
* Parameters:
29-
* string s: a string
30-
* int k: the length of the substrings to find
31-
*
32-
* Returns
33-
* string: the string ' + "\n" + ' where and are the two substrings
34-
*/
35-
public String[] getSmallestAndLargestArray(String s, int k) {
36-
String smallest = "", largest = "";
37-
38-
// Make a substring of lenght k.
39-
// Sort the String array
40-
// smallest = array[0]
41-
// largest = array[len-1]
42-
43-
int i, j;
44-
int stringLength = s.length();
45-
ArrayList<String> subStringList
46-
= new ArrayList<String>();
47-
48-
for (i = 0; i < s.length()-2 ; i++) {
49-
subStringList.add(s.substring(i, k+i));
50-
}
51-
52-
Collections.sort(subStringList);
53-
smallest = subStringList.get(0);
54-
largest = subStringList.get(subStringList.size()-1);
5524

56-
return new String[]{smallest, largest};
57-
}
5825

59-
@Test
60-
public void shouldReturnSmallestAndLargestSubstring() {
61-
String s = "welcometojava";
6226

63-
int k = 3;
64-
String[] actual = getSmallestAndLargestArray(s, k);
65-
String [] expected = new String[]{"ava", "wel"};
66-
67-
Assertions.assertArrayEquals(expected, actual);
68-
69-
}
7027

7128
@Test
7229
public void main() {

0 commit comments

Comments
 (0)