File tree 4 files changed +103
-0
lines changed
main/java/com/thealgorithms/datastructures
test/java/com/thealgorithms/strings
4 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,54 @@ public boolean detectLoop() {
54
54
return false ;
55
55
}
56
56
57
+ /**
58
+ * Swaps nodes of two given values a and b.
59
+ *
60
+ */
61
+ public void swapNodes (int valueFirst , int valueSecond ) {
62
+ if (valueFirst == valueSecond ){
63
+ return ;
64
+ }
65
+ Node previousA = null ,currentA = head ;
66
+ while (currentA != null && currentA .value != valueFirst ){
67
+ previousA = currentA ;
68
+ currentA = currentA .next ;
69
+ }
70
+
71
+ Node previousB = null ,currentB = head ;
72
+ while (currentB != null && currentB .value != valueSecond ){
73
+ previousB = currentB ;
74
+ currentB = currentB .next ;
75
+ }
76
+ /** If either of 'a' or 'b' is not present, then return */
77
+ if (currentA == null || currentB == null ){
78
+ return ;
79
+ }
80
+
81
+ // If 'a' is not head node of list
82
+ if (previousA != null ){
83
+ previousA .next = currentB ;
84
+ }
85
+ else {
86
+ // make 'b' as the new head
87
+ head = currentB ;
88
+ }
89
+
90
+ // If 'b' is not head node of list
91
+ if (previousB != null ){
92
+ previousB .next = currentA ;
93
+ }
94
+ else {
95
+ // Make 'a' as new head
96
+ head = currentA ;
97
+ }
98
+ // Swap next pointer
99
+
100
+ Node temp = currentA .next ;
101
+ currentA .next = currentB .next ;
102
+ currentB .next = temp ;
103
+ }
104
+
57
105
/**
58
106
* Reverse a singly linked list from a given node till the end
59
107
*
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ public static void main(String[] args) throws Exception {
38
38
Scanner sc = new Scanner (System .in );
39
39
String str = sc .nextLine ();
40
40
System .out .println (check (str ));
41
+ sc .close ();
41
42
}
42
43
43
44
}
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .strings ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+
5
+ import static org .junit .jupiter .api .Assertions .*;
6
+
7
+
8
+ public class AlphabeticalTest {
9
+ @ Test
10
+ public void isAlphabetical () {
11
+ // expected to be true
12
+ String input1 = "abcdefghijklmno" ;
13
+ String input2 = "abcdxxxyzzzz" ;
14
+ String input3 = "fpw" ;
15
+
16
+ // expected to be false
17
+ String input4 = "123a" ;
18
+ String input5 = "abcABC" ;
19
+ String input6 = "abcdefghikjlmno" ;
20
+
21
+ assertTrue (Alphabetical .isAlphabetical (input1 ));
22
+ assertTrue (Alphabetical .isAlphabetical (input2 ));
23
+ assertTrue (Alphabetical .isAlphabetical (input3 ));
24
+
25
+ assertFalse (Alphabetical .isAlphabetical (input4 ));
26
+ assertFalse (Alphabetical .isAlphabetical (input5 ));
27
+ assertFalse (Alphabetical .isAlphabetical (input6 ));
28
+ }
29
+
30
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .strings ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+
5
+ import static org .junit .jupiter .api .Assertions .*;
6
+
7
+
8
+ public class PangramTest {
9
+ @ Test
10
+ public void isPangram () {
11
+ String fullAlphabet = "abcdefghijklmnopqrstuvwxyz" ;
12
+ String notFullAlphabet = "abcdefghiklmnopqrstuvwxyz" ;
13
+ String fullMixedCaseAlphabet = "a BCDE fghIjkLMnop qrSTuv WXYz" ;
14
+ String sentence1 = "The quick brown fox jumps over the lazy dog" ;
15
+ String sentence2 = "The quick brown fox jumps over the lazy gentleman" ; // missing letter d
16
+
17
+ assertTrue (Pangram .isPangram (fullAlphabet ));
18
+ assertFalse (Pangram .isPangram (notFullAlphabet ));
19
+ assertTrue (Pangram .isPangram (fullMixedCaseAlphabet ));
20
+ assertTrue (Pangram .isPangram (sentence1 ));
21
+ assertFalse (Pangram .isPangram (sentence2 ));
22
+
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments