Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2dcee64

Browse files
committed
Added isunique from ctci
1 parent e7ddd9b commit 2dcee64

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

src/main/java/com/rampatra/misc/RightShiftOperator.java renamed to src/main/java/com/rampatra/bits/RightShiftOperator.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
package com.rampatra.misc;
1+
package com.rampatra.bits;
22

33
/**
4-
* Created by IntelliJ IDEA.
4+
* {@code >>} shifts bits to right filling left bits with the left most
5+
* bit (most significant bit). Also called signed right shift.
6+
* {@code >>>} shifts bits to the right filling left bits
7+
* with 0. Also called unsigned right shift.
58
*
6-
* @author: ramswaroop
7-
* @date: 6/2/15
8-
* @time: 3:26 PM
9-
*/
10-
11-
/**
12-
* {@code >>} shifts bits to right filling left bits with the left most bit (most significant bit)
13-
* {@code >>>} shifts bits to the right filling left bits with 0
9+
* @author rampatra
10+
* @since 6/2/15
1411
*/
1512
public class RightShiftOperator {
1613

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rampatra.bits;
2+
3+
/**
4+
* Understanding shifting in Java. What happens when you shift a number by a negative number.
5+
*
6+
* @author rampatra
7+
* @link https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
8+
* @link https://stackoverflow.com/a/10516830/1385441
9+
* @since 18/11/2018
10+
*/
11+
public class ShiftByNegativeNumber {
12+
/**
13+
* These both are the same as in java if the left operand is int only the low five bits are considered
14+
* for shifting and if the left operand is a long then the low six bits are considered.
15+
* Binary representation of -2 is 11.....11110. The last 5 bits are 11110 which is 30.
16+
*
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
int v1 = 1 << -2;
21+
int v2 = 1 << 30;
22+
System.out.println(v1);
23+
System.out.println(v2);
24+
}
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.rampatra.ctci;
2+
3+
/**
4+
* @author rampatra
5+
* @since 18/11/2018
6+
*/
7+
public class IsUnique {
8+
9+
private static boolean hasAllUniqueCharacters(String str) {
10+
if (str == null || str.length() > 128) return false;
11+
12+
boolean[] charSet = new boolean[128]; // assuming the string contains only ASCII characters
13+
for (int i = 0; i < str.length(); i++) {
14+
int charVal = str.charAt(i);
15+
if (charSet[charVal]) {
16+
return false;
17+
}
18+
charSet[charVal] = true;
19+
}
20+
return true;
21+
}
22+
23+
private static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase(String s) {
24+
int checker = 0;
25+
for (int i = 0; i < s.length(); i++) {
26+
int charValue = s.charAt(i) - 'a';
27+
if ((checker & (1 << charValue)) > 0) {
28+
return false;
29+
}
30+
checker |= (1 << charValue);
31+
}
32+
return true;
33+
}
34+
35+
public static void main(String[] args) {
36+
String s = "ram";
37+
System.out.println(hasAllUniqueCharacters(s));
38+
s = "rama";
39+
System.out.println(hasAllUniqueCharacters(s));
40+
s = "ramA";
41+
System.out.println(hasAllUniqueCharacters(s));
42+
System.out.println("-------");
43+
s = "ram";
44+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
45+
s = "rama";
46+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
47+
// not working as the input contains different cases
48+
s = "ramA";
49+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
50+
}
51+
}

0 commit comments

Comments
 (0)