FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_131/LeetCode_242_131.cpp 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_131
/
LeetCode_242_131.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (41 loc) · 1.22 KB
Breadcrumbs
algorithm
/
Week_02
/
id_131
/
LeetCode_242_131.cpp
Copy path
File metadata and controls
47 lines (41 loc) · 1.22 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
/*
* 有效的字母异位词
* 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
* 输入: s = "anagram", t = "nagaram" 输出: true
* 输入: s = "rat", t = "car" 输出: false
*/
class
Solution
{
public:
bool
isAnagram
(string s, string t) {
if
(s.
size
() != t.
size
())
return
false
;
int
strCount[
26
] = {
0
};
for
(
int
indexStr =
0
; indexStr < s.
size
(); ++indexStr)
{
strCount[s[indexStr]-
'
a
'
] +=
1
;
strCount[t[indexStr]-
'
a
'
] -=
1
;
}
for
(
int
indexC =
0
; indexC <
26
; ++indexC)
{
if
(strCount[indexC] !=
0
)
return
false
;
}
return
true
;
}
bool
isAnagramB
(string s, string t)
{
if
(s.
size
() != t.
size
()) {
return
false
;
}
//
假设字符串只包含小写字母
int
alphabet[
26
] = {
0
};
//
统计字符串s中小写字母出现的次数
for
(
int
indexStr =
0
; indexStr < s.
size
(); ++indexStr){
alphabet[alphabet[indexStr]-
'
a
'
]++;
}
//
长度固定,所有字母出现的次数必须都相同
for
(
int
indexStr =
0
; indexStr < t.
size
(); ++indexStr){
if
(--alphabet[alphabet[indexStr] -
'
a
'
] <
0
)
return
false
;
}
return
true
;
}
};
Back
|
FazBrowse Home
|
New Git URL