- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decrypt String from Alphabet to Integer Mapping in Python
Suppose we have a string s that is formed by digits ('0' - '9') and '#'. We have to map s to one English lowercase characters as follows −
Characters ('a' to 'i') are represented by ('1' to '9') respectively.
Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
We have to find the string formed after mapping. We are taking one assumption that a unique mapping will always exist. So if the input is like “10#11#12”, then it will be “jkab”. As 10# is j, 11# is k, 1 is a and 2 is b.
To solve this, we will follow these steps −
create one map to hold all characters and their corresponding ASCII values
ans := 0, and map[‘’] := ‘’, ad i := length of string – 1
while i > 0
if s[i] is #, then
temp := “”
for j := i – 2 to i, temp := temp + s[j]
ans := map[temp] + ans
decrease i by 3
otherwise ans := map[s[i]] + ans, and decrease i by 1
return ans
Example (Python)
Let us see the following implementation to get a better understanding −
class Solution(object): def freqAlphabets(self, s): m = {} x = 'a' for i in range(1, 27): m[str(i)] = x x = chr(ord(x) + 1) ans = "" m['']='' i = len(s) - 1 while i >= 0: if s[i] == "#": temp = "" for j in range(i - 2, i): temp += s[j] ans = m[str(temp)] + ans i -= 3 else: ans = m[s[i]] + ans i -= 1 return ans ob1 = Solution() print(ob1.freqAlphabets("17#123#5621#"))
Input
"17#123#5621#"
Output
qawefu
- Related Articles
- Python – Character indices Mapping in String List
- Map an integer from decimal base to hexadecimal with custom mapping JavaScript
- How to get integer values from a string in Python?
- Mapping string to Numerals in JavaScript
- Encrypt and Decrypt a string in MySQL?
- How to encrypt and decrypt data in Python
- Program to find n length string made of letters from m sized alphabet with no palindrome in Python
- Program to decrypt code to defuse the bomb in Python
- Java Program to convert from integer to String
- Java Program to convert from String to integer
- Python – Convert Integer Matrix to String Matrix
- Python Mapping Types
- How to convert String to Integer and Integer to String in Java?
- Python – Assign Alphabet to each element
- Mapping unique characters of string to an array - JavaScript
