Skip to content

Commit f7bc082

Browse files
committed
Update palindromic substring
1 parent 3a248cc commit f7bc082

File tree

8 files changed

+76
-42
lines changed

8 files changed

+76
-42
lines changed

README.md

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

9595
7. Longest palindromic substring: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.js) [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.md)
9696

97-
8. Palindromic substrings: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/palindromicStrings.js)
97+
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

9999
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)
100100

src/java1/algorithms/strings/longestPalindromicSubstring/LongestPalindromicSubstring.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Given a string `str`, return the longest palindromic substring of `str`.
55

66
### Examples
77
Example 1:
8-
Input: s = "ababd"
8+
Input: str = "ababd"
99
Output: "aba"
1010

1111
Example 2:
12-
Input: s = "dbbc"
12+
Input: str = "dbbc"
1313
Output: "bb"
1414

1515
**Algorithmic Steps**

src/java1/algorithms/strings/palindromicSubstrings/PalindromicSubstrings.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ private static int countSubstrings(String str) {
99

1010
int count = 0;
1111
for(int i=0; i < str.length(); i++) {
12-
count += countPalindrome(str, i, i);
13-
count += countPalindrome(str, i, i+1);
12+
count += countPalindromes(str, i, i);
13+
count += countPalindromes(str, i, i+1);
1414
}
1515
return count;
1616
}
1717

18-
private static int countPalindrome(String str, int left, int right) {
18+
private static int countPalindromes(String str, int left, int right) {
1919

2020
int count = 0;
2121
while(left >=0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030
String str = "baaab";
3131
System.out.println(countSubstrings(str));
3232

33-
String str1 = "abc";
33+
String str1 = "abcd";
3434
System.out.println(countSubstrings(str1));
3535

3636
String str2 = "aaa";
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1+
**Description:**
2+
Given a string `str`, return the number of substrings within`str` that are palindromes.
3+
4+
**Note:**A palindrome is a string that reads the same backward as forward.
5+
6+
### Examples
7+
Example 1:
8+
Input: str = "abcd"
9+
Output: 4
10+
11+
Example 2:
12+
Input: str = "aaa"
13+
Output: 3
14+
15+
116
**Algorithmic Steps**
2-
This problem is solved with the help of Two pointer approach(Expand around center). The algorithmic approach can be summarized as follows:
17+
This problem is solved with the help of **Expand around center** approach. The algorithmic approach can be summarized as follows:
318

4-
1. Write a preliminary check by returning the input string length when its size is less than 2.
19+
1. Write a preliminary check by just returning the input string length when its size is less than 2.
520

6-
2. Initialize count variable to 0, to store the palindromic substrings count.
21+
2. Initialize count(`count`) variable to 0, which is used to store the palindromic substrings count.
722

8-
3. Iterate over the input string using an iteration variable i(from 0 to length of string).
23+
3. Iterate over the input string using an iteration variable `i`(from 0 to end of string).
924

10-
4. For each iteration, calculate the total count of odd palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` as left and right pointer values.
25+
4. For each iteration, calculate the total count of odd palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` for both left and right pointer values.
1126

1227
5. For each iteration, calculate the total count of even palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` as left and `i+1` as right pointer values.
1328

14-
6. The common function iterate over the given string using starting(i.e, left) and ending(i.e, right) index arguments. The left pointer is going to be decremented and right pointer is going to be incremented whenever the respective character values are equal.
29+
6. The common function is created to iterate over the given string using starting(i.e, `left`) and ending(i.e, `right`) index arguments. The left pointer is going to be decremented and right pointer is going to be incremented whenever the respective character values are equal.
1530

16-
7. The sum of even and odd palindromic substring count is going to be returned as total count for palindromic substrings.
31+
7. The sum of even and odd palindromic substring's count is going to be returned as total count for palindromic substrings.
1732

1833

1934
**Time and Space complexity:**
20-
This algorithm has a time complexity of O(n * 2)( O(n * 2) + O(n * 2)) because either odd or event need to iterate over the input string atleast once which takes O(n) and substrings calculate on each position takes O(n). Also, it takes space complexity of O(1) without using any data structure.
35+
This algorithm has a time complexity of `O(n * 2)`(i.e, `O(n * 2) + O(n * 2)`) because finding both odd and event length palindrome takes `O(n) +O(n)` time complexity and it needs to be repeated on each character position which takes O(n).
36+
37+
Also, it takes space complexity of `O(1)` without using any additional data structure.

src/javascript/algorithms/strings/7.longestPalindromicSubstring/longestPalindromicSubstring.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Given a string `str`, return the longest palindromic substring of `str`.
55

66
### Examples
77
Example 1:
8-
Input: s = "ababd"
8+
Input: str = "ababd"
99
Output: "aba"
1010

1111
Example 2:
12-
Input: s = "dbbc"
12+
Input: str = "dbbc"
1313
Output: "bb"
1414

1515
**Algorithmic Steps**

src/javascript/algorithms/strings/palindromicSubstrings/palindromicStrings.js src/javascript/algorithms/strings/8.palindromicSubstrings/palindromicSubstrings.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//Expand around center:- TC:O(n*2) SC:O(1)
22

3-
function countPalindromicStrings(str) {
3+
function countPalindromicSubstrings(str) {
44

55
if(str.length < 2) return str.length;
66

@@ -23,10 +23,10 @@ function countPalindromes(str, left, right) {
2323
}
2424

2525
let str = "baaab";
26-
console.log(countPalindromicStrings(str));
26+
console.log(countPalindromicSubstrings(str));
2727

28-
let str1 = "abc";
29-
console.log(countPalindromicStrings(str1));
28+
let str1 = "abcd";
29+
console.log(countPalindromicSubstrings(str1));
3030

3131
let str2 = "aaa";
32-
console.log(countPalindromicStrings(str2));
32+
console.log(countPalindromicSubstrings(str2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description:**
2+
Given a string `str`, return the number of substrings within`str` that are palindromes.
3+
4+
**Note:**A palindrome is a string that reads the same backward as forward.
5+
6+
### Examples
7+
Example 1:
8+
Input: str = "abcd"
9+
Output: 4
10+
11+
Example 2:
12+
Input: str = "aaa"
13+
Output: 3
14+
15+
16+
**Algorithmic Steps**
17+
This problem is solved with the help of **Expand around center** approach. The algorithmic approach can be summarized as follows:
18+
19+
1. Write a preliminary check by just returning the input string length when its size is less than 2.
20+
21+
2. Initialize count(`count`) variable to 0, which is used to store the palindromic substrings count.
22+
23+
3. Iterate over the input string using an iteration variable `i`(from 0 to end of string).
24+
25+
4. For each iteration, calculate the total count of odd palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` for both left and right pointer values.
26+
27+
5. For each iteration, calculate the total count of even palindromic substrings using a separate common function(step6). This common function accepts current iteration variable `i` as left and `i+1` as right pointer values.
28+
29+
6. The common function is created to iterate over the given string using starting(i.e, `left`) and ending(i.e, `right`) index arguments. The left pointer is going to be decremented and right pointer is going to be incremented whenever the respective character values are equal.
30+
31+
7. The sum of even and odd palindromic substring's count is going to be returned as total count for palindromic substrings.
32+
33+
34+
**Time and Space complexity:**
35+
This algorithm has a time complexity of `O(n * 2)`(i.e, `O(n * 2) + O(n * 2)`) because finding both odd and event length palindrome takes `O(n) +O(n)` time complexity and it needs to be repeated on each character position which takes O(n).
36+
37+
Also, it takes space complexity of `O(1)` without using any additional data structure.

src/javascript/algorithms/strings/palindromicSubstrings/palindromicSubstrings.md

-20
This file was deleted.

0 commit comments

Comments
 (0)