Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7bd0fc

Browse files
authored
Merge pull requestneetcode-gh#106 from rahulshishodia/valid-anagram
Updated 242-Valid-Anagram.java
2 parents0c45772 +b2e438c commitb7bd0fc

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

‎java/242-Valid-Anagram.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
classSolution {
22
publicbooleanisAnagram(Strings,Stringt) {
3-
if (s.length() !=t.length())returnfalse;
43

5-
int[]countS =newint[26];
6-
int[]countT =newint[26];
4+
if(s.length() !=t.length())returnfalse;
5+
if(s.equals(t))returntrue;
76

8-
for (inti =0 ;i <s.length() ;i++) {
9-
countS[s.charAt(i) -'a']++;
10-
countT[t.charAt(i) -'a']++;
11-
}
7+
Map<Character,Integer>sMap =newHashMap<>();
8+
Map<Character,Integer>tMap =newHashMap<>();
129

13-
for (inti =0 ;i <26 ;i++) {
14-
if (countS[i] -countT[i] !=0)returnfalse;
10+
for(inti =0;i <s.length();i++) {
11+
sMap.merge(s.charAt(i),1,Integer::sum);
12+
tMap.merge(t.charAt(i),1,Integer::sum);
1513
}
1614

15+
for(Characterc :sMap.keySet()) {
16+
if(!sMap.get(c).equals(tMap.get(c)))returnfalse;
17+
}
1718
returntrue;
1819
}
19-
}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp