FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_26/Leetcode_242_26.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_02
/
id_26
/
Leetcode_242_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
88 lines (75 loc) · 2.66 KB
Breadcrumbs
algorithm
/
Week_02
/
id_26
/
Leetcode_242_26.java
Copy path
File metadata and controls
88 lines (75 loc) · 2.66 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
package
com
.
fanlu
.
leetcode
.
hashtable
;
// Source : https://leetcode.com/problems/valid-anagram/
// Id : 242
// Author : Fanlu Hai
// Date : 2018-04-22
// Other : anagram noun. a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
// Tips : 1.Use int[] with ascii as hash table; 2.use 'for i certain numbers' instead of 'for reach'.
public
class
ValidAnagram
{
//38.34% 70.24%
public
boolean
isAnagramSlow
(
String
s
,
String
t
) {
if
(
null
==
s
||
null
==
t
)
return
false
;
if
(
s
.
length
() !=
t
.
length
())
return
false
;
int
[]
letters
=
new
int
[
26
];
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
int
tmpS
=
s
.
charAt
(
i
) -
'a'
;
int
tmpT
=
t
.
charAt
(
i
) -
'a'
;
letters
[
tmpS
]++;
letters
[
tmpT
]--;
}
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
letters
[
i
] !=
0
)
return
false
;
}
return
true
;
}
// try to remove tmp variables to see if it runs faster
// And it does
// 72.38% 70.94%
public
boolean
isAnagramFast
(
String
s
,
String
t
) {
if
(
null
==
s
||
null
==
t
)
return
false
;
if
(
s
.
length
() !=
t
.
length
())
return
false
;
int
[]
letters
=
new
int
[
26
];
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
letters
[
s
.
charAt
(
i
) -
'a'
]++;
letters
[
t
.
charAt
(
i
) -
'a'
]--;
}
for
(
int
i
:
letters
) {
if
(
letters
[
i
] !=
0
)
return
false
;
}
return
true
;
}
// try to remove tmp variables to see if it runs faster
// And it does
// use for i instead of for reach, it becomes even faster
// 90.46% 71.14%
public
boolean
isAnagram
(
String
s
,
String
t
) {
if
(
null
==
s
||
null
==
t
)
return
false
;
if
(
s
.
length
() !=
t
.
length
())
return
false
;
int
[]
letters
=
new
int
[
26
];
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
letters
[
s
.
charAt
(
i
) -
'a'
]++;
letters
[
t
.
charAt
(
i
) -
'a'
]--;
}
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
letters
[
i
] !=
0
)
return
false
;
}
return
true
;
}
public
static
void
main
(
String
[]
args
) {
ValidAnagram
validAnagram
=
new
ValidAnagram
();
System
.
out
.
println
(
validAnagram
.
isAnagram
(
"abcdefg"
,
"abcdefg"
));
System
.
out
.
println
(
validAnagram
.
isAnagram
(
"abcdefg"
,
"abcdefgg"
));
System
.
out
.
println
(
validAnagram
.
isAnagram
(
"abcdefg"
,
"gbcdefa"
));
System
.
out
.
println
(
validAnagram
.
isAnagram
(
"aaaaaaa"
,
"aaaaaba"
));
System
.
out
.
println
(
validAnagram
.
isAnagram
(
null
,
null
));
}
}
Back
|
FazBrowse Home
|
New Git URL