FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
crackingCodingInterview/Strings.java at master · kumarravi/crackingCodingInterview · GitHub
kumarravi
crackingCodingInterview
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
crackingCodingInterview
/
Strings.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (75 loc) · 1.73 KB
Breadcrumbs
crackingCodingInterview
/
Strings.java
Copy path
File metadata and controls
91 lines (75 loc) · 1.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package
cracking
.
coding
;
public
class
Strings
{
public
static
void
main
(
String
[]
args
){
//isUniqueCharString("prabhu");
//isPermute("ravi","vira");
//System.out.println(Integer.bitCount((3^5)));
//System.out.println(convertDoubletoBinary(0.7));
System
.
out
.
println
(
checkbits
(
1
,
3
));
}
public
static
int
isUniqueCharString
(
String
str
){
int
checker
=
0
;
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++){
int
val
=
str
.
charAt
(
i
) -
'a'
;
if
((
checker
& (
1
<<
val
)) >
0
){
System
.
out
.
println
(
"Not Unique"
);
return
0
;
}
checker
|= (
1
<<
val
);
}
System
.
out
.
println
(
"Unique"
);
return
1
;
}
public
static
int
isPermute
(
String
str1
,
String
str2
){
if
(
str1
.
length
()!=
str2
.
length
()){
System
.
out
.
println
(
"not equal"
);
return
-
1
;
}
int
checker1
=
getStringBits
(
str1
);
int
checker2
=
getStringBits
(
str2
);
if
(
checker1
!=
checker2
){
System
.
out
.
println
(
"Not Permutation"
);
}
else
System
.
out
.
println
(
"Permutaion"
);
return
1
;
}
public
static
int
getStringBits
(
String
str
){
int
checker
=
0
;
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++){
int
val
=
str
.
charAt
(
i
)-
'a'
;
checker
|= (
1
<<
val
);
}
return
checker
;
}
public
static
String
convertDoubletoBinary
(
double
num
){
if
(
num
>=
1
||
num
<=
0
){
return
"ERROR1"
;
}
System
.
out
.
println
(
num
);
StringBuilder
binary
=
new
StringBuilder
();
double
frac
=
0.5
;
binary
.
append
(
"."
);
while
(
num
>
0
){
if
(
binary
.
length
()>
32
)
return
"ERROR"
;
if
(
num
>=
frac
){
binary
.
append
(
1
);
num
-=
frac
;
}
else
{
binary
.
append
(
0
);
}
frac
/=
2
;
}
return
binary
.
toString
();
}
public
static
int
checkbits
(
int
a
,
int
b
){
int
xor
=
a
^
b
;
int
count
=
0
;
while
(
xor
>
0
){
if
((
xor
&
1
)>
0
)
count
++;
xor
>>=
1
;
}
return
count
;
}
}
Back
|
FazBrowse Home
|
New Git URL