Skip to content

Commit 32e53b5

Browse files
solves largest substring between two equal characters
1 parent ca3d456 commit 32e53b5

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-326/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-326/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-328/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-328/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -403,7 +403,7 @@
403403
| 1608 | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x) | [![Java](assets/java.png)](src/SpecialArrayWithXElementsGreaterThanEqualToX.java) | |
404404
| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses) | [![Java](assets/java.png)](src/MaximumNestingDepthOfTheParentheses.java) | |
405405
| 1619 | [Mean of Array After Removing Some Elements](https://leetcode.com/problems/mean-of-array-after-removing-some-elements) | [![Java](assets/java.png)](src/MeanOfArrayAfterRemovingSomeElements.java) | |
406-
| 1624 | [Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters) | | |
406+
| 1624 | [Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters) | [![Java](assets/java.png)](src/LargestSubStringBetweenTwoEqualCharacters.java) | |
407407
| 1629 | [Slowest Key](https://leetcode.com/problems/slowest-key) | | |
408408
| 1636 | [Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency) | | |
409409
| 1640 | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class LargestSubStringBetweenTwoEqualCharacters {
5+
public int maxLengthBetweenEqualCharacters(String s) {
6+
int maxLength = -1;
7+
final Map<Character, Integer> lastIndex = new HashMap<>();
8+
for (int i = 0 ; i < s.length() ; i++) {
9+
if (lastIndex.containsKey(s.charAt(i))) {
10+
maxLength = Math.max(maxLength, i - lastIndex.get(s.charAt(i)) - 1);
11+
} else {
12+
lastIndex.put(s.charAt(i), i);
13+
}
14+
}
15+
return maxLength;
16+
}
17+
}

0 commit comments

Comments
 (0)