Skip to content

Commit 4d39810

Browse files
committed
Simplified the code
1 parent 3abc42e commit 4d39810

File tree

4 files changed

+27
-39
lines changed

4 files changed

+27
-39
lines changed

src/main/java/com/rampatra/linkedlists/CloneWithRandPointers.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedLi
2525

2626
// copy each node and insert after it
2727
while (curr != null) {
28-
DoubleLinkedNode<E> newNode = new DoubleLinkedNode<>(null, curr.item, curr.next);
29-
curr.next = newNode;
28+
curr.next = new DoubleLinkedNode<>(null, curr.item, curr.next);
3029
curr = curr.next.next;
3130
}
3231

@@ -53,10 +52,10 @@ public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedLi
5352

5453
public static void main(String[] args) {
5554
DoubleLinkedList<Integer> linkedList = new DoubleLinkedList<>();
56-
linkedList.add(00);
57-
linkedList.add(11);
58-
linkedList.add(22);
59-
linkedList.add(33);
55+
linkedList.add(0);
56+
linkedList.add(1);
57+
linkedList.add(2);
58+
linkedList.add(3);
6059
linkedList.getNode(0).prev = null;
6160
linkedList.getNode(1).prev = linkedList.getNode(2);
6261
linkedList.getNode(2).prev = linkedList.getNode(0);

src/main/java/com/rampatra/strings/AnagramsTogether.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*
1212
* @author rampatra
1313
* @since 9/23/15
14-
* @time: 8:11 PM
1514
*/
1615
public class AnagramsTogether {
1716

@@ -24,7 +23,7 @@ public class AnagramsTogether {
2423
*
2524
* @param s
2625
*/
27-
public static void printAnagramsTogether(String[] s) {
26+
private static void printAnagramsTogether(String[] s) {
2827

2928
// each key holds all the indexes of a anagram
3029
HashMap<String, List<Integer>> hashMap = new HashMap<>();

src/main/java/com/rampatra/strings/AnagramsTogetherLexicographically.java

+13-18
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@
1212
public class AnagramsTogetherLexicographically {
1313

1414
/**
15-
* Takes an array of String {@param s} and prints anagrams in groups where the groups
15+
* Takes an array of String {@code strings} and prints anagrams in groups where the groups
1616
* are arranged lexicographically and the strings within each group are also arranged
1717
* lexicographically.
1818
*
19-
* @param s
19+
* @param strings
2020
*/
21-
public static void printAnagramsTogether(String[] s) {
21+
public static void printAnagramsTogether(String[] strings) {
2222

2323
HashMap<String, List<Integer>> hashMap = new HashMap<>();
24-
TreeSet<List<String>> treeSet = new TreeSet<>(new Comparator() {
25-
@Override
26-
public int compare(Object o1, Object o2) {
27-
if (o1 instanceof List<?> && o2 instanceof List<?>) {
28-
return ((List<String>) o1).get(0).compareTo(((List<String>) o2).get(0));
29-
} else {
30-
return 0;
31-
}
24+
TreeSet<List<String>> treeSet = new TreeSet<>((Comparator) (o1, o2) -> {
25+
if (o1 instanceof List<?> && o2 instanceof List<?>) {
26+
return ((List<String>) o1).get(0).compareTo(((List<String>) o2).get(0));
27+
} else {
28+
return 0;
3229
}
3330
});
3431

35-
for (int i = 0; i < s.length; i++) {
36-
String removeSpaces = s[i].replaceAll("\\s+", "");
32+
for (int i = 0; i < strings.length; i++) {
33+
String removeSpaces = strings[i].replaceAll("\\s+", "");
3734
char[] chars = removeSpaces.toCharArray();
3835
Arrays.sort(chars);
3936

@@ -50,17 +47,14 @@ public int compare(Object o1, Object o2) {
5047
List<String> anagrams = new ArrayList<>();
5148

5249
for (int i = 0; i < entry.getValue().size(); i++) {
53-
anagrams.add(s[entry.getValue().get(i)]);
50+
anagrams.add(strings[entry.getValue().get(i)]);
5451
}
5552

5653
Collections.sort(anagrams); // arrange anagrams lexicographically within a single line
5754
treeSet.add(anagrams); // sort the entire output lexicographically
5855
}
5956

60-
Iterator iterator = treeSet.iterator();
61-
while (iterator.hasNext()) {
62-
System.out.println(iterator.next());
63-
}
57+
treeSet.stream().flatMap(Collection::stream).forEach(System.out::println);
6458
}
6559

6660
/**
@@ -72,6 +66,7 @@ public static void main(String[] args) {
7266
Scanner in = new Scanner(System.in);
7367
List<String> strings = new ArrayList<>();
7468
String s;
69+
System.out.println("Input string in separate lines (blank string to stop):");
7570
// you should use in.hasNextLine()
7671
while (!(s = in.nextLine()).trim().equals("")) {
7772
strings.add(s);

src/main/java/com/rampatra/strings/KeepOnlyKConsecutiveLetters.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,22 @@ public class KeepOnlyKConsecutiveLetters {
1818
*
1919
* @param str input string
2020
* @param k
21-
* @return
21+
* @return a string with at most {@code k} consecutive characters
2222
*/
2323
private static String keepOnlyKConsecutiveLetters(String str, int k) {
2424
StringBuilder sb = new StringBuilder();
25+
int count = 0;
2526

2627
for (int i = 0; i < str.length(); i++) {
27-
char ch = str.charAt(i);
28-
29-
for (int j = 0; i + j < str.length() && j < k; j++) {
30-
char ch2 = str.charAt(i + j);
31-
if (ch2 == ch) {
32-
sb.append(ch2);
33-
} else {
34-
break;
35-
}
28+
if (i != 0 && str.charAt(i) != str.charAt(i - 1)) {
29+
count = 0;
3630
}
37-
38-
while (i + 1 < str.length() && str.charAt(i + 1) == str.charAt(i)) {
39-
i++;
31+
if (count < k) {
32+
sb.append(str.charAt(i));
33+
count++;
4034
}
4135
}
36+
4237
return sb.toString();
4338
}
4439

0 commit comments

Comments
 (0)