FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/algorithms/bit/find_difference.py at master · Mu-L/algorithms-python · GitHub
Mu-L
/
algorithms-python
Public
forked from
keon/algorithms
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
algorithms-python
/
algorithms
/
bit
/
find_difference.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
28 lines (25 loc) · 747 Bytes
Breadcrumbs
algorithms-python
/
algorithms
/
bit
/
find_difference.py
Copy path
File metadata and controls
28 lines (25 loc) · 747 Bytes
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
"""
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter
at a random position. Find the letter that was added in t.
For example:
Input:
s = "abcd"
t = "abecd"
Output: 'e'
Explanation:
'e' is the letter that was added.
"""
"""
We use the characteristic equation of XOR.
A xor B xor C = A xor C xor B
If A == C, then A xor C = 0
and then, B xor 0 = B
"""
def
find_difference
(
s
,
t
):
ret
=
0
for
ch
in
s
+
t
:
# ord(ch) return an integer representing the Unicode code point of that character
ret
=
ret
^
ord
(
ch
)
# chr(i) Return the string representing a character whose Unicode code point is the integer i
return
chr
(
ret
)
Back
|
FazBrowse Home
|
New Git URL