FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/PuzzleCoding/src/StringAnagram.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
StringAnagram.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (54 loc) · 1.61 KB
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
StringAnagram.java
Copy path
File metadata and controls
66 lines (54 loc) · 1.61 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
import
java
.
util
.
Arrays
;
public
class
StringAnagram
{
public
static
void
main
(
String
[]
args
) {
// String s1 = "apple";
// String s2 = "palpe";
String
s1
=
"tiger"
;
String
s2
=
"tgrie"
;
System
.
out
.
println
(
isAnagram
(
s1
,
s2
));
System
.
out
.
println
(
isPermutation
(
s1
,
s2
));
}
public
static
String
stringSort
(
String
s
) {
char
[]
a
=
s
.
toCharArray
();
Arrays
.
sort
(
a
);
return
new
String
(
a
);
}
public
static
String
stringSort1
(
String
s
) {
char
[]
letter
=
new
char
[
256
];
char
[]
a
=
s
.
toCharArray
();
for
(
char
c
:
a
) {
letter
[
c
]++;
}
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
letter
.
length
;
i
++) {
if
(
letter
[
i
] >
0
) {
while
(
letter
[
i
] >
0
) {
sb
.
append
((
char
)
i
);
letter
[
i
]--;
}
}
}
return
sb
.
toString
();
}
public
static
boolean
isAnagram
(
String
s1
,
String
s2
) {
return
stringSort
(
s1
).
equals
(
stringSort1
(
s2
));
}
public
static
boolean
isPermutation
(
String
s1
,
String
s2
) {
if
(
s1
.
length
() !=
s2
.
length
())
return
false
;
char
[]
letter
=
new
char
[
256
];
char
[]
a1
=
s1
.
toCharArray
();
for
(
char
c
:
a1
) {
letter
[
c
]++;
}
char
[]
a2
=
s2
.
toCharArray
();
for
(
char
c
:
a2
) {
letter
[
c
]--;
}
for
(
int
i
=
0
;
i
<
letter
.
length
; ++
i
) {
if
((
letter
[
i
] >
0
) || (
letter
[
i
] <
0
))
return
false
;
}
return
true
;
}
}
Back
|
FazBrowse Home
|
New Git URL