Skip to content

Commit d9899b8

Browse files
author
Algorithmica
committed
uploading pending code
1 parent 9f9d65d commit d9899b8

File tree

10 files changed

+208
-30
lines changed

10 files changed

+208
-30
lines changed

2017-september/12.string problems/src/com/alg/top20/palsubstr/LongPalSubString.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,37 @@ public static int longPalSubstr2(String s) {
5050
printMemory(mem);
5151
return max;
5252
}
53+
//TC:O(n^2)
54+
//SC:O(1)
55+
//Expand around each index as centre
56+
public static int longPalSubstr3(String s) {
57+
int max = 0;
58+
for(int i = 0; i < s.length(); ++i) {
59+
int odd = expandCenter(i, i, s);
60+
int even = expandCenter(i, i+1, s);
61+
max = Math.max(max, Math.max(odd, even));
62+
}
63+
return max;
64+
}
65+
private static int expandCenter(int l, int r, String s) {
66+
while(l >= 0 && r < s.length()) {
67+
if(s.charAt(l) == s.charAt(r)) {
68+
--l;
69+
++r;
70+
} else
71+
break;
72+
}
73+
return r-l-1;
74+
}
5375

5476
private static void printMemory(boolean[][] mem) {
5577
for(boolean[] tmp: mem)
5678
System.out.println(Arrays.toString(tmp));
5779
}
5880

5981
public static void main(String[] args) {
60-
System.out.println(longPalSubstr2(args[0]));
82+
//System.out.println(longPalSubstr2(args[0]));
83+
System.out.println(longPalSubstr3(args[0]));
6184
}
6285

6386
}

2017-september/12.string problems/src/com/alg/top20/palsubstr/PalSubString.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class AllSubsets {
4+
5+
public static void allSubsets(int[] in) {
6+
int n = in.length;
7+
int limit = 1 << n;
8+
for(int i = 0; i < limit; ++i) {
9+
for(int j = 1; j <= n; ++j) {
10+
if( (i & (1<<j-1)) != 0)
11+
System.out.print(in[j-1]+" ");
12+
}
13+
System.out.println();
14+
}
15+
}
16+
public static void main(String[] args) {
17+
int n = Integer.parseInt(args[0]);
18+
int[] in = new int[n];
19+
for(int i = 0; i < n; ++i)
20+
in[i] = i+10;
21+
allSubsets(in);
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class BitWiseUtilities {
4+
5+
public static void display(int n) {
6+
int mask = 1 << 31;
7+
for(int i = 0; i < 32; ++i) {
8+
if((n & mask) != 0)
9+
System.out.print("1");
10+
else
11+
System.out.print("0");
12+
mask = mask >>> 1;
13+
}
14+
System.out.println();
15+
}
16+
17+
public static void main(String[] args) {
18+
int n = Integer.parseInt(args[0]);
19+
display(n);
20+
}
21+
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class CountOnes {
4+
5+
public static int countOnes1(int n) {
6+
int mask = 1 << 31;
7+
int count = 0;
8+
for(int i = 0; i < 32; ++i) {
9+
if((n & mask) != 0)
10+
++count;
11+
mask = mask >>> 1;
12+
}
13+
return count;
14+
}
15+
16+
public static int countOnes2(int n) {
17+
int mask = 0xF;
18+
int count = 0;
19+
int[] ones = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
20+
while(n > 0) {
21+
count = count + ones[(n & mask)];
22+
n = n >>> 4;
23+
}
24+
return count;
25+
}
26+
27+
public static int countOnes3(int n) {
28+
int count = 0;
29+
while(n > 0) {
30+
++count;
31+
n = n & (n-1);
32+
}
33+
return count;
34+
}
35+
public static void main(String[] args) {
36+
int n = Integer.parseInt(args[0]);
37+
BitWiseUtilities.display(n);
38+
System.out.println(countOnes1(n));
39+
System.out.println(countOnes2(n));
40+
System.out.println(countOnes3(n));
41+
}
42+
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class EvenOddCheck {
4+
5+
public static boolean isOdd(int n) {
6+
return (n & 1) != 0;
7+
}
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
11+
}
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class HigherLowerMultiples {
4+
5+
public static int nextHighestMult8(int n) {
6+
return (n+7) & ~7;
7+
}
8+
9+
public static int prevLowestMult8(int n) {
10+
return n & ~7;
11+
}
12+
public static void main(String[] args) {
13+
int n = Integer.parseInt(args[0]);
14+
System.out.println(prevLowestMult8(n));
15+
System.out.println(nextHighestMult8(n));
16+
}
17+
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class IPAddress {
4+
5+
public static int getNetworkId(int n) {
6+
int mask = 0xFF000000;
7+
return (n & mask) >>> 24;
8+
}
9+
public static int getHostId(int n) {
10+
int mask = 0xFFFFFF;
11+
return n & mask;
12+
}
13+
14+
public static void main(String[] args) {
15+
int n = Integer.parseInt(args[0]);
16+
BitWiseUtilities.display(n);
17+
System.out.println(getNetworkId(n));
18+
System.out.println(getHostId(n));
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class IsPowerOf2 {
4+
5+
public static boolean isPowerOf2(int n) {
6+
return (n & (n-1)) == 0;
7+
}
8+
public static void main(String[] args) {
9+
int n = Integer.parseInt(args[0]);
10+
BitWiseUtilities.display(n);
11+
System.out.println(isPowerOf2(n));
12+
13+
}
14+
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.alg.top20.bittricks;
2+
3+
public class Operators {
4+
5+
public static int set(int n, int k) {
6+
n = n | (1<<k-1);
7+
return n;
8+
}
9+
10+
public static int clear(int n, int k) {
11+
n = n & ~(1<<k-1);
12+
return n;
13+
}
14+
15+
public static int toggle(int n, int k) {
16+
n = n ^ (1<<k-1);
17+
return n;
18+
}
19+
20+
public static void main(String[] args) {
21+
int n = Integer.parseInt(args[0]);
22+
BitWiseUtilities.display(n);
23+
BitWiseUtilities.display(set(n, 20));
24+
BitWiseUtilities.display(toggle(n, 4));
25+
BitWiseUtilities.display(toggle(n, 3));
26+
BitWiseUtilities.display(clear(n, 2));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)