Skip to content

Commit 02409a5

Browse files
authored
Merge branch 'master' into master
2 parents bb2a301 + d53c2ce commit 02409a5

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,54 @@ public boolean detectLoop() {
5454
return false;
5555
}
5656

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+
57105
/**
58106
* Reverse a singly linked list from a given node till the end
59107
*

src/main/java/com/thealgorithms/datastructures/stacks/DuplicateBrackets.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static void main(String[] args) throws Exception {
3838
Scanner sc = new Scanner(System.in);
3939
String str = sc.nextLine();
4040
System.out.println(check(str));
41+
sc.close();
4142
}
4243

4344
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)