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

Commiteaf8d38

Browse files
authored
Merge pull requestneetcode-gh#1440 from Mahim1997/dev/205_Java_Swift
205. Isomorphic Strings
2 parentsfb5b707 +f6e4521 commiteaf8d38

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

‎java/205-Isomorphic-Strings.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
classSolution {
2+
publicbooleanisIsomorphic(Strings,Stringt) {
3+
// Needs bidirectional mapping from s <--> t
4+
if(s.length() !=t.length()) {
5+
returnfalse;
6+
}
7+
8+
Map<Character,Character>mapSourceToDest =newHashMap<>();
9+
Map<Character,Character>mapDestToSource =newHashMap<>();
10+
11+
intlen =s.length();
12+
for(inti=0;i<len;i++) {
13+
charsourceChar =s.charAt(i),destChar =t.charAt(i);
14+
charsourceReturn =mapSourceToDest.getOrDefault(sourceChar,destChar);
15+
if(sourceReturn !=destChar) {
16+
returnfalse;
17+
}
18+
mapSourceToDest.put(sourceChar,destChar);
19+
20+
chardestReturn =mapDestToSource.getOrDefault(destChar,sourceChar);
21+
if(destReturn !=sourceChar) {
22+
returnfalse;
23+
}
24+
mapDestToSource.put(destChar,sourceChar);
25+
}
26+
27+
returntrue;
28+
}
29+
}

‎swift/205-Isomorphic-Strings.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution{
2+
func isIsomorphic(_ s:String, _ t:String)->Bool{
3+
guard s.count== t.countelse{returnfalse}
4+
5+
varmapSourceToDest:[Character:Character]=[:]
6+
varmapDestToSource:[Character:Character]=[:]
7+
8+
for(sourceChar, destChar)inzip(s, t){
9+
iflet sourceMapped=mapSourceToDest[sourceChar]{
10+
guard sourceMapped== destCharelse{returnfalse}
11+
}else{
12+
mapSourceToDest[sourceChar]= destChar
13+
}
14+
15+
iflet destMapped=mapDestToSource[destChar]{
16+
guard destMapped== sourceCharelse{returnfalse}
17+
}else{
18+
mapDestToSource[destChar]= sourceChar
19+
}
20+
}
21+
22+
returntrue
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp