Skip to content

Commit d37ab2b

Browse files
committed
Update encoding and decoding problem
1 parent f7bc082 commit d37ab2b

File tree

7 files changed

+166
-65
lines changed

7 files changed

+166
-65
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ List of Programs related to data structures and algorithms
9696

9797
8. Palindromic substrings: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/8.palindromicSubstrings/palindromicSubstrings.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/8.palindromicSubstrings/palindromicSubstrings.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/8.palindromicSubstrings/palindromicSubstrings.md)
9898

99-
9. Encode and decode strings: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/encodeDecodeStrings.js)
99+
9. Encode and decode strings: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/9.encodeDecodeStrings/encodeDecodeStrings.md)
100100

101101
10. Greatest common devisor of strings: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/greatestCommonDivisor.js)
102102

src/java1/algorithms/strings/EncodeDecodeStrings.java

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//TC: O(n) SC: O(n)
2+
package java1.algorithms.strings.encodeDecodeStrings;
3+
4+
import java.util.*;
5+
6+
public class EncodeDecodeStrings {
7+
private static String encodeString(String[] strs) {
8+
String encodedStr = "";
9+
for(String str: strs) {
10+
encodedStr += str.length()+"#"+str;
11+
}
12+
return encodedStr;
13+
}
14+
15+
private static List<String> decodeString(String str) {
16+
List<String> decodedStringsList = new ArrayList<>();
17+
18+
int i=0;
19+
while(i < str.length()) {
20+
int j = i;
21+
while(str.charAt(j) != '#') j++;
22+
int start = j+1;
23+
int wordLength = Integer.parseInt(str, i, j, 10);
24+
String subStr = str.substring(start, start+wordLength);
25+
decodedStringsList.add(subStr);
26+
i = start+wordLength;
27+
}
28+
return decodedStringsList;
29+
}
30+
31+
public static void main(String[] args) {
32+
String[] strs1 = {"learn", "datastructure", "algorithms", "easily"};
33+
String encodedStr1 = encodeString(strs1);
34+
System.out.println(encodedStr1);
35+
System.out.println(decodeString(encodedStr1));
36+
37+
String[] strs2 = {"one", "two", "three"};
38+
String encodedStr2 = encodeString(strs2);
39+
System.out.println(encodedStr2);
40+
System.out.println(decodeString(encodedStr2));
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
**Description:**
2+
Write an algorithm to encode a list of strings to a single string. After that, the encoded string is decoded back to the original list of strings. Please implement `encode` and `decode`
3+
4+
### Examples
5+
Example 1:
6+
Input: ["learn", "datastructure", "algorithms", "easily"]
7+
Output: ["learn", "datastructure", "algorithms", "easily"]
8+
9+
Example 2:
10+
Input: ["one", "two", "three"]
11+
Output: ["one", "two", "three"]
12+
13+
14+
**Algorithmic Steps**
15+
This problem is solved with the help of basic string and array built-in operations. The algorithmic approach can be summarized as follows:
16+
17+
**Encode string**
18+
1. Initialize an empty string(`encodedStr`) to store the encoded string.
19+
20+
2. Iterate over the array of strings. For each string iteration, add a prefix with the combination of string length and special(`#`) symbol before each word/string.
21+
22+
3. Return the encoded string as an output.
23+
24+
**Decode string**
25+
26+
1. Initialize an empty array(`decodedStringsList`) to store the decoded string.
27+
28+
2. Initialize an index variable(`i`) to 0, which is used to iterate over the input string.
29+
30+
3. Iterate over an input string until the end of its length.
31+
32+
4. Create a temporary variable(`j`) which is assigned to index variable.
33+
34+
5. Skip the prefix related to each string's length until the character reaches special(`#`) symbol. The number of characters ignored are indicated by incremental value of `j`. The next character position(`j+1`) indicates the beggining of a string.
35+
36+
6. Calculate the word length followed by substring using the index variables.
37+
38+
7. Add each substring to the decoded string list. Also, increment the index variable to be used for the next substring.
39+
40+
8. Repeat steps 4-7 until all the strings decoded and return the decoded strings.
41+
42+
**Time and Space complexity:**
43+
This algorithm has a time complexity of `O(n)` for both encoding and decoding functions, where `n` is the number of total characters for each string in the input array. This is because we need to iterate over each string in the array to perform constant-time operations for encoding operation. In the same way, we need iterate over all the characters of a encoded string for decoding operation.
44+
45+
Also, it takes space complexity of `O(n)` to store all the characters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//TC: O(n) SC: O(n)
2+
function encodeStrings(strs) {
3+
let encodedStr = "";
4+
for (let str of strs) {
5+
encodedStr += str.length + "#" + str;
6+
}
7+
return encodedStr;
8+
}
9+
10+
function decodeString(str) {
11+
let decodedStrArr = [];
12+
let i = 0;
13+
while (i < str.length) {
14+
let j = i;
15+
while (str[j] != "#") j++;
16+
let start = j+1;
17+
let wordLength = Number.parseInt(str.substring(i, j));
18+
let subStr = str.substring(start, start + wordLength);
19+
decodedStrArr.push(subStr);
20+
i = start + wordLength;
21+
}
22+
return decodedStrArr;
23+
}
24+
25+
let strs1 = ["learn", "datastructure", "algorithms", "easily"];
26+
let encodedStr1 = encodeStrings(strs1);
27+
console.log(encodedStr1);
28+
console.log(decodeString(encodedStr1));
29+
30+
let strs2 = ["one", "two", "three"];
31+
let encodedStr2 = encodeStrings(strs2);
32+
console.log(encodedStr2);
33+
console.log(decodeString(encodedStr2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
**Description:**
2+
Write an algorithm to encode a list of strings to a single string. After that, the encoded string is decoded back to the original list of strings. Please implement `encode` and `decode`
3+
4+
### Examples
5+
Example 1:
6+
Input: ["learn", "datastructure", "algorithms", "easily"]
7+
Output: ["learn", "datastructure", "algorithms", "easily"]
8+
9+
Example 2:
10+
Input: ["one", "two", "three"]
11+
Output: ["one", "two", "three"]
12+
13+
14+
**Algorithmic Steps**
15+
This problem is solved with the help of basic string and array built-in operations. The algorithmic approach can be summarized as follows:
16+
17+
**Encode string**
18+
1. Initialize an empty string(`encodedStr`) to store the encoded string.
19+
20+
2. Iterate over the array of strings. For each string iteration, add a prefix with the combination of string length and special(`#`) symbol before each word/string.
21+
22+
3. Return the encoded string as an output.
23+
24+
**Decode string**
25+
26+
1. Initialize an empty array(`decodedStrArr`) to store the decoded string.
27+
28+
2. Initialize an index variable(`i`) to 0, which is used to iterate over the input string.
29+
30+
3. Iterate over an input string until the end of its length.
31+
32+
4. Create a temporary variable(`j`) which is assigned to index variable.
33+
34+
5. Skip the prefix related to each string's length until the character reaches special(`#`) symbol. The number of characters ignored are indicated by incremental value of `j`. The next character position(`j+1`) indicates the beggining of a string.
35+
36+
6. Calculate the word length followed by substring using the index variables.
37+
38+
7. Add each substring to the decoded string array. Also, increment the index variable to be used for the next substring.
39+
40+
8. Repeat steps 4-7 until all the strings decoded and return the decoded strings.
41+
42+
**Time and Space complexity:**
43+
This algorithm has a time complexity of `O(n)` for both encoding and decoding functions, where `n` is the number of total characters for each string in the input array. This is because we need to iterate over each string in the array to perform constant-time operations for encoding function. In the same way, we need iterate over all the characters of a encoded string for decoding function.
44+
45+
Also, it takes space complexity of `O(n)` to store all the characters.

src/javascript/algorithms/strings/encodeDecodeStrings.js

-27
This file was deleted.

0 commit comments

Comments
 (0)