Solved Decrypt String Challenge in Java with Iterative Approach

Day 88 of #100DaysOfCode Solved Decrypt String from Alphabet to Integer Mapping in Java 🔡 Approach Today's challenge involved mapping a string of digits to lowercase English characters, where single digits '1' through '9' map to 'a' through 'i', and two digits followed by a '#' (e.g., '10#', '11#') map to 'j' through 'z'. I used an iterative approach with a while loop and an index i to traverse the input string. I checked ahead to see if a two-digit mapping existed by looking for a '#' at s.charAt(i + 2). If a '#' was found, I parsed the two-digit number (e.g., "10", "11") using s.substring(i, i + 2), converted it to an integer, and then mapped it to the correct character by adding it to the ASCII value of 'a' and subtracting one. I then advanced the index i by 3. If no '#' was found, it meant the current character s.charAt(i) was a single-digit mapping. I converted this digit to an integer and mapped it to a character similarly, then advanced the index i by 1. This approach processes the string from left to right, correctly handling the two-digit encodings first, which resulted in a quick runtime that beat 79.30% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #StringManipulation #ProblemSolving

  • graphical user interface

To view or add a comment, sign in

Explore content categories