Skip to content

Commit cc348a9

Browse files
committed
making anagrams: done, some minor javadoc error fixed
1 parent b58efea commit cc348a9

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hackerrank.algorithms.strings;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author ramswaroop
7+
* @version 28/09/2016
8+
*/
9+
public class MakingAnagrams {
10+
11+
/**
12+
* Find number of characters to be deleted to make {@param a}
13+
* and {@param b} anagrams.
14+
* See: https://www.hackerrank.com/challenges/making-anagrams
15+
*
16+
* @param a
17+
* @param b
18+
* @return
19+
*/
20+
public static int makeAnagrams(String a, String b) {
21+
22+
int i = 0, j = 0, c = 0;
23+
char[] s1 = a.toCharArray();
24+
char[] s2 = b.toCharArray();
25+
Arrays.sort(s1);
26+
Arrays.sort(s2);
27+
28+
while (i < s1.length || j < s2.length) {
29+
if (i >= s1.length) {
30+
c++;
31+
j++;
32+
} else if (j >= s2.length) {
33+
c++;
34+
i++;
35+
} else if (s1[i] < s2[j]) {
36+
c++;
37+
i++;
38+
} else if (s1[i] > s2[j]) {
39+
c++;
40+
j++;
41+
} else {
42+
i++;
43+
j++;
44+
}
45+
}
46+
47+
return c;
48+
}
49+
50+
public static void main(String[] a) {
51+
System.out.println(makeAnagrams("abc", "cde"));
52+
}
53+
}

src/me/ramswaroop/arrays/RotateArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public static void rotateNaiveApproach(int[] a, int k) {
4646
* Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3]
4747
* Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]
4848
* NOTE: Ar = Reverse of A
49+
* See: http://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/
4950
*
5051
* @param a
5152
* @param k
52-
* @see: http://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/
5353
*/
5454
public static void rotateReversal(int[] a, int k) {
5555
ReverseArray.reverseRecursive(a, 0, k - 1);
@@ -59,10 +59,10 @@ public static void rotateReversal(int[] a, int k) {
5959

6060
/**
6161
* Juggling algorithm for array rotation.
62+
* See: http://www.geeksforgeeks.org/array-rotation/
6263
*
6364
* @param a
6465
* @param k
65-
* @see: http://www.geeksforgeeks.org/array-rotation/
6666
*/
6767
public static void rotateGCD(int[] a, int k) {
6868
int gcd = gcd(a.length, k), temp, i, j, p;

0 commit comments

Comments
 (0)