Java Program to Expand Letters Based on Following Digits in a String- Interview Question asked in Deloitte, Infomerica
The provided Java code snippet expands a compressed string based on the rules defined by the input format. Let's go through the logic step-by-step:
Purpose of the Code:
The code aims to decode a string where a letter is followed by a digit that specifies how many times the letter should be repeated. For example, given the input string `"d2r3w4t2"`, the output will be `"ddrrrwwwwtt"`.
### Key Components of the Code:
1. **Initialization:**
- `str`: The input string to be decoded (`"d2r3w4t2"`).
- `StringBuilder sb`: A dynamic string object to build the decoded output.
- `char[] ch`: An array of characters from the input string to facilitate character-by-character processing.
- `int i`: A loop counter initialized to zero, used to track the current character index.
2. **Main Loop (`for` loop):**
- The loop iterates through each character in the `ch` array except the last one (`j < ch.length - 1`), processing characters to construct the decoded string.
- **If the current character `ch[j]` is a digit:**
- Convert the character to an integer to determine the repeat count (`value`).
- Subtract 1 from the digit (`Integer.parseInt(String.valueOf(ch[j])) - 1`) because we assume that the character should appear an additional time to the count of repetitions.
- Append the previous character (`ch[j-1]`) `value` times to the `StringBuilder`.
- **If the current character `ch[j]` is not a digit:**
- Directly append the character to `StringBuilder`.
3. **Handling the Last Character:**
- After the loop completes, the last character is handled separately (`if (Character.isDigit(ch[i]))`) to ensure no character is skipped.
- If it's a digit, the same logic is applied: calculate the repeat count and append the previous character the required number of times.
- If it's not a digit, append the character directly to `StringBuilder`.
4. **Output:**
- The constructed string in `StringBuilder sb` is printed to the console.
### Example Breakdown:
Given `str = "d2r3w4t2"`:
- **`d2`**: The digit `2` indicates `d` should be repeated once more (2-1 times). Result: `dd`
- **`r3`**: The digit `3` indicates `r` should be repeated twice more (3-1 times). Result: `ddrrr`
- **`w4`**: The digit `4` indicates `w` should be repeated three more times (4-1 times). Result: `ddrrrwwww`
- **`t2`**: The digit `2` indicates `t` should be repeated once more (2-1 times). Result: `ddrrrwwwwtt`
### Final Output:
The output of the code will be:
Purpose of the Code:
The code aims to decode a string where a letter is followed by a digit that specifies how many times the letter should be repeated. For example, given the input string `"d2r3w4t2"`, the output will be `"ddrrrwwwwtt"`.
### Key Components of the Code:
1. **Initialization:**
- `str`: The input string to be decoded (`"d2r3w4t2"`).
- `StringBuilder sb`: A dynamic string object to build the decoded output.
- `char[] ch`: An array of characters from the input string to facilitate character-by-character processing.
- `int i`: A loop counter initialized to zero, used to track the current character index.
2. **Main Loop (`for` loop):**
- The loop iterates through each character in the `ch` array except the last one (`j < ch.length - 1`), processing characters to construct the decoded string.
- **If the current character `ch[j]` is a digit:**
- Convert the character to an integer to determine the repeat count (`value`).
- Subtract 1 from the digit (`Integer.parseInt(String.valueOf(ch[j])) - 1`) because we assume that the character should appear an additional time to the count of repetitions.
- Append the previous character (`ch[j-1]`) `value` times to the `StringBuilder`.
- **If the current character `ch[j]` is not a digit:**
- Directly append the character to `StringBuilder`.
3. **Handling the Last Character:**
- After the loop completes, the last character is handled separately (`if (Character.isDigit(ch[i]))`) to ensure no character is skipped.
- If it's a digit, the same logic is applied: calculate the repeat count and append the previous character the required number of times.
- If it's not a digit, append the character directly to `StringBuilder`.
4. **Output:**
- The constructed string in `StringBuilder sb` is printed to the console.
### Example Breakdown:
Given `str = "d2r3w4t2"`:
- **`d2`**: The digit `2` indicates `d` should be repeated once more (2-1 times). Result: `dd`
- **`r3`**: The digit `3` indicates `r` should be repeated twice more (3-1 times). Result: `ddrrr`
- **`w4`**: The digit `4` indicates `w` should be repeated three more times (4-1 times). Result: `ddrrrwwww`
- **`t2`**: The digit `2` indicates `t` should be repeated once more (2-1 times). Result: `ddrrrwwwwtt`
### Final Output:
The output of the code will be:
Comments
Post a Comment