You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: java/twopointers/IsPalindromeValid.java
+7-8Lines changed: 7 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -28,13 +28,12 @@ public boolean isPalindromeValid(String s) {
28
28
}
29
29
30
30
publicstaticvoidmain(String[] args) {
31
-
System.out.println("Tests an empty string. Expected output: " + newIsPalindromeValid().isPalindromeValid(""));
32
-
System.out.println("Tests a single-character string. Expected output: " + newIsPalindromeValid().isPalindromeValid("a"));
33
-
System.out.println("Tests a palindrome with two characters. Expected output: " + newIsPalindromeValid().isPalindromeValid("aa"));
34
-
System.out.println("Tests a non-palindrome with two characters. Expected output: " + newIsPalindromeValid().isPalindromeValid("ab"));
35
-
System.out.println("Tests a string with no alphanumeric characters. Expected output: " + newIsPalindromeValid().isPalindromeValid("!, (?)"));
36
-
System.out.println("Tests a palindrome with punctuation and numbers. Expected output: " + newIsPalindromeValid().isPalindromeValid("21.02.2021"));
37
-
System.out.println("Tests a non-palindrome with punctuation. Expected output: " + newIsPalindromeValid().isPalindromeValid("hello, world!"));
38
-
31
+
System.out.println("Tests an empty string.\n Input: s=\"\"\n Expected output: true\n Actual output: " + newIsPalindromeValid().isPalindromeValid(""));
32
+
System.out.println("Tests a single-character string.\n Input: s=\"a\"\n Expected output: true\n Actual output: " + newIsPalindromeValid().isPalindromeValid("a"));
33
+
System.out.println("Tests a palindrome with two characters.\n Input: s=\"aa\"\n Expected output: true\n Actual output: " + newIsPalindromeValid().isPalindromeValid("aa"));
34
+
System.out.println("Tests a non-palindrome with two characters.\n Input: s=\"ab\"\n Expected output: false\n Actual output: " + newIsPalindromeValid().isPalindromeValid("ab"));
35
+
System.out.println("Tests a string with no alphanumeric characters.\n Input: s=\"!, (?)\"\n Expected output: true\n Actual output: " + newIsPalindromeValid().isPalindromeValid("!, (?)"));
36
+
System.out.println("Tests a palindrome with punctuation and numbers.\n Input: s=\"21.02.2021\"\n Expected output: false\n Actual output: " + newIsPalindromeValid().isPalindromeValid("21.02.2021"));
37
+
System.out.println("Tests a non-palindrome with punctuation.\n Input: s=\"hello, world!\"\n Expected output: false\n Actual output: " + newIsPalindromeValid().isPalindromeValid("hello, world!"));
Copy file name to clipboardExpand all lines: java/twopointers/LargestContainer.java
+29-10Lines changed: 29 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -12,22 +12,41 @@ public int largestContainer(int[] height) {
12
12
// If both lines have the same height, move both pointers inward.
13
13
if (height[left] < height[right]) {
14
14
left++;
15
-
} elseif (height[left] > height[right]) {
16
-
right--;
17
15
} else {
18
-
left++;
19
16
right--;
20
17
}
21
18
}
22
19
returnmaxWater;
23
20
}
24
21
25
22
publicstaticvoidmain(String[] args) {
26
-
System.out.println("Tests an empty array. Expected output: " + newLargestContainer().largestContainer(newint[]{}));
27
-
System.out.println("Tests an array with one element. Expected output: " + newLargestContainer().largestContainer(newint[]{1}));
28
-
System.out.println("Tests an array with no containers that can contain water. Expected output: " + newLargestContainer().largestContainer(newint[]{0, 1, 0}));
29
-
System.out.println("Tests an array with all heights are the same. Expected output: " + newLargestContainer().largestContainer(newint[]{3, 3, 3, 3}));
30
-
System.out.println("Tests an array with strictly increasing heights. Expected output: " + newLargestContainer().largestContainer(newint[]{1, 2, 3}));
31
-
System.out.println("Tests an array with strictly decreasing heights. Expected output: " + newLargestContainer().largestContainer(newint[]{3, 2, 1}));
32
-
}
23
+
System.out.println("Tests an empty array.\n" +
24
+
" Input: height=[]\n" +
25
+
" Expected output: 0\n" +
26
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{}));
27
+
28
+
System.out.println("Tests an array with one element.\n" +
29
+
" Input: height=[1]\n" +
30
+
" Expected output: 0\n" +
31
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{1}));
32
+
33
+
System.out.println("Tests an array with no containers that can contain water.\n" +
34
+
" Input: height=[0, 1, 0]\n" +
35
+
" Expected output: 0\n" +
36
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{0, 1, 0}));
37
+
38
+
System.out.println("Tests an array where all heights are the same.\n" +
39
+
" Input: height=[3, 3, 3, 3]\n" +
40
+
" Expected output: 9\n" +
41
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{3, 3, 3, 3}));
42
+
43
+
System.out.println("Tests an array with strictly increasing heights.\n" +
44
+
" Input: height=[1, 2, 3]\n" +
45
+
" Expected output: 2\n" +
46
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{1, 2, 3}));
47
+
48
+
System.out.println("Tests an array with strictly decreasing heights.\n" +
49
+
" Input: height=[3, 2, 1]\n" +
50
+
" Expected output: 2\n" +
51
+
" Actual output: " + newLargestContainer().largestContainer(newint[]{3, 2, 1})); }
Copy file name to clipboardExpand all lines: java/twopointers/LargestContainerBruteForce.java
+29-7Lines changed: 29 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -17,11 +17,33 @@ public int largestContainerBruteForce(int[] height) {
17
17
}
18
18
19
19
publicstaticvoidmain(String[] args) {
20
-
System.out.println("Tests an empty array. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{}));
21
-
System.out.println("Tests an array with one element. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{1}));
22
-
System.out.println("Tests an array with no containers that can contain water. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{0, 1, 0}));
23
-
System.out.println("Tests an array with all heights are the same. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{3, 3, 3, 3}));
24
-
System.out.println("Tests an array with strictly increasing heights. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{1, 2, 3}));
25
-
System.out.println("Tests an array with strictly decreasing heights. Expected output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{3, 2, 1}));
26
-
}
20
+
System.out.println("Tests an empty array.\n" +
21
+
" Input: height=[]\n" +
22
+
" Expected output: 0\n" +
23
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{}));
24
+
25
+
System.out.println("Tests an array with one element.\n" +
26
+
" Input: height=[1]\n" +
27
+
" Expected output: 0\n" +
28
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{1}));
29
+
30
+
System.out.println("Tests an array with no containers that can contain water.\n" +
31
+
" Input: height=[0, 1, 0]\n" +
32
+
" Expected output: 0\n" +
33
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{0, 1, 0}));
34
+
35
+
System.out.println("Tests an array where all heights are the same.\n" +
36
+
" Input: height=[3, 3, 3, 3]\n" +
37
+
" Expected output: 9\n" +
38
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{3, 3, 3, 3}));
39
+
40
+
System.out.println("Tests an array with strictly increasing heights.\n" +
41
+
" Input: height=[1, 2, 3]\n" +
42
+
" Expected output: 2\n" +
43
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{1, 2, 3}));
44
+
45
+
System.out.println("Tests an array with strictly decreasing heights.\n" +
46
+
" Input: height=[3, 2, 1]\n" +
47
+
" Expected output: 2\n" +
48
+
" Actual output: " + newLargestContainerBruteForce().largestContainerBruteForce(newint[]{3, 2, 1})); }
System.out.println("Tests a string with a random pivot character.\n" +
56
+
" Input: s='ynitsed'\n" +
57
+
" Expected output: 'ynsdeit'\n" +
58
+
" Actual output: " + newNextLexicographicalSequence().nextLexicographicalSequence("ynitsed"));
75
59
76
-
// Concatenate the prefix and reversed suffix and return
77
-
returnprefix + suffix;
78
-
}
60
+
System.out.println("Tests a string with a single character.\n" +
61
+
" Input: s='a'\n" +
62
+
" Expected output: 'a'\n" +
63
+
" Actual output: " + newNextLexicographicalSequence().nextLexicographicalSequence2("a"));
79
64
80
-
publicstaticvoidmain(String[] args) {
81
-
System.out.println("Tests a string with a single character. Expected result: 'a'. Actual result : " +newNextLexicographicalSequence().nextLexicographicalSequence("a"));
82
-
System.out.println("Tests a string with a repeated character. Expected result: 'aaaa'. Actual result : " +newNextLexicographicalSequence().nextLexicographicalSequence("aaaa"));
83
-
System.out.println("Tests a string with a random pivot character. Expected result: 'ynsdeit'. Actual result : " + newNextLexicographicalSequence().nextLexicographicalSequence("ynitsed"));
65
+
System.out.println("Tests a string with a repeated character.\n" +
66
+
" Input: s='aaaa'\n" +
67
+
" Expected output: 'aaaa'\n" +
68
+
" Actual output: " + newNextLexicographicalSequence().nextLexicographicalSequence2("aaaa"));
84
69
85
-
System.out.println("Tests a string with a single character. Expected result: 'a'. Actual result : " +newNextLexicographicalSequence().nextLexicographicalSequence2("a"));
86
-
System.out.println("Tests a string with a repeated character. Expected result: 'aaaa'. Actual result : " +newNextLexicographicalSequence().nextLexicographicalSequence2("aaaa"));
87
-
System.out.println("Tests a string with a random pivot character. Expected result: 'ynsdeit'. Actual result : " +newNextLexicographicalSequence().nextLexicographicalSequence2("ynitsed"));
88
-
}
70
+
System.out.println("Tests a string with a random pivot character.\n" +
71
+
" Input: s='ynitsed'\n" +
72
+
" Expected output: 'ynsdeit'\n" +
73
+
" Actual output: " + newNextLexicographicalSequence().nextLexicographicalSequence2("ynitsed")); }
Copy file name to clipboardExpand all lines: java/twopointers/PairSumSorted.java
+34-8Lines changed: 34 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -20,12 +20,38 @@ public int[] pairSumSorted(int[] nums, int target) {
20
20
}
21
21
22
22
publicstaticvoidmain(String[] args) {
23
-
System.out.println("Tests an empty array. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{}, 0)));
24
-
System.out.println("Tests an array with one element. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{1}, 1)));
25
-
System.out.println("Tests a two-element array that contains a pair that sums to the target. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 3}, 5)));
26
-
System.out.println("Tests a two-element array that does not contain a pair that sums to the target. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 4}, 5)));
27
-
System.out.println("Tests an array with duplicated values. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 2, 3}, 5)));
28
-
System.out.println("Tests if the algorithm works with a negative number in the target pair. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{-1, 2, 3}, 2)));
29
-
System.out.println("Tests if the algorithm works with both numbers of the target pair being negative. Expected output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{-3, -2, -1}, -5)));
30
-
}
23
+
System.out.println("Tests an empty array.\n" +
24
+
" Input: nums=[], target=0\n" +
25
+
" Expected output: []\n" +
26
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{}, 0)));
27
+
28
+
System.out.println("Tests an array with one element.\n" +
29
+
" Input: nums=[1], target=1\n" +
30
+
" Expected output: []\n" +
31
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{1}, 1)));
32
+
33
+
System.out.println("Tests a two-element array that contains a pair that sums to the target.\n" +
34
+
" Input: nums=[2, 3], target=5\n" +
35
+
" Expected output: [0, 1]\n" +
36
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 3}, 5)));
37
+
38
+
System.out.println("Tests a two-element array that does not contain a pair that sums to the target.\n" +
39
+
" Input: nums=[2, 4], target=5\n" +
40
+
" Expected output: []\n" +
41
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 4}, 5)));
42
+
43
+
System.out.println("Tests an array with duplicated values.\n" +
44
+
" Input: nums=[2, 2, 3], target=5\n" +
45
+
" Expected output: [0, 2]\n" +
46
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{2, 2, 3}, 5)));
47
+
48
+
System.out.println("Tests if the algorithm works with a negative number in the target pair.\n" +
49
+
" Input: nums=[-1, 2, 3], target=2\n" +
50
+
" Expected output: [0, 2]\n" +
51
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{-1, 2, 3}, 2)));
52
+
53
+
System.out.println("Tests if the algorithm works with both numbers of the target pair being negative.\n" +
54
+
" Input: nums=[-3, -2, -1], target=-5\n" +
55
+
" Expected output: [0, 1]\n" +
56
+
" Actual output: " + Arrays.toString(newPairSumSorted().pairSumSorted(newint[]{-3, -2, -1}, -5))); }
0 commit comments