python/CodeChallenge_32.py

33 lines
1.1 KiB
Python

'''
A string consisting only of the characters b, d and w is painted on a glass window of a store. Alex walks past the store, standing directly in front of the glass
window, and observes string s. Alex then heads inside the store, looks directly at the same glass window, and observes string r.
Alex gives you string s. Your task is to find and output string r.
Example 1
Input:
dwd
Output:
bwb
Example 2
Input:
bbwwwddd
Output:
bbbwwwdd
'''
class Solution(object):
def solve(self, s:str) -> str:
r = []
s = s[::-1] #reverse the string
# Create a translation table for characters
for char in s:
trans = str.maketrans({'b':'d','d':'b','w':'w'})
return s.translate(trans)
# Example usage:
if __name__ == "__main__":
solution = Solution()
print(solution.solve("dwd")) # Output: "bwb"
print(solution.solve("bbwwwddd")) # Output: "bbbwwwdd"
print(solution.solve("bwd")) # Output: "dwb"
print(solution.solve("d")) # Output: "b"
print(solution.solve("billyjoebob")) #Output: "dodeojyllid"