-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
76. Minimum Window Substring - Java 233. Number of Digit One - Java
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# LeetCode 233.Number of Digit One | ||
|
||
|
||
## Problem Description | ||
|
||
Given an integer `n`, count the total number of digit `1` appearing in all non-negative integers less than or equal to `n`. | ||
|
||
**Example 1:** | ||
|
||
Input: n = 13 | ||
Output: 6 | ||
|
||
**Example 2:** | ||
|
||
Input: n = 0 | ||
Output: 0 | ||
|
||
**Constraints:** | ||
|
||
`0 <= n <= 109` | ||
|
||
### Link | ||
|
||
https://leetcode.com/problems/number-of-digit-one/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public int countDigitOne(int n) { | ||
int count = 0; | ||
|
||
for (long i = 1; i <= n; i *= 10) { | ||
long divider = i * 10; | ||
count += (n / divider) * i + Math.min(Math.max(n % divider - i + 1, 0), i); | ||
} | ||
|
||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# LeetCode 76.Minimum Window Substring | ||
|
||
## Problem Description | ||
|
||
Given two strings `s` and `t` of lengths `m` and `n` respectively, return the minimum window substring of `s` such that every character in `t` (including duplicates) is included in the window. If there is no such substring, return the empty string `""`. | ||
|
||
The testcases will be generated such that the answer is **unique.** | ||
|
||
Example 1: | ||
|
||
Input: s = "ADOBECODEBANC", t = "ABC" | ||
Output: "BANC" | ||
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. | ||
|
||
Example 2: | ||
|
||
Input: s = "a", t = "a" | ||
Output: "a" | ||
Explanation: The entire string s is the minimum window. | ||
|
||
Example 3: | ||
|
||
Input: s= "a", t = "aa" | ||
Output:"" | ||
Explanation: Both 'a's from t must be included in the window. | ||
Since the largest window of s only has one 'a', return empty string. | ||
|
||
**Constraints:** | ||
|
||
- `m == s.length` | ||
- `n == t.length` | ||
- `1 <= m, n <= 105` | ||
- `s` and `t` consist of uppercase and lowercase English letters. | ||
|
||
Follow up: Could you find an algorithm that runs in `O(m + n)` time? | ||
|
||
### Link | ||
|
||
https://leetcode.com/problems/minimum-window-substring/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.HashMap; | ||
|
||
class Solution { | ||
public String minWindow(String s, String t) { | ||
if (s == null || t == null || s.length() < t.length()) { | ||
return ""; | ||
} | ||
|
||
|
||
HashMap<Character, Integer> tMap = new HashMap<>(); | ||
for (char c : t.toCharArray()) { | ||
tMap.put(c, tMap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
int left = 0; | ||
int minLen = Integer.MAX_VALUE; | ||
int minLeft = 0; | ||
int count = tMap.size(); | ||
|
||
for (int right = 0; right < s.length(); right++) { | ||
char rightChar = s.charAt(right); | ||
|
||
if (tMap.containsKey(rightChar)) { | ||
tMap.put(rightChar, tMap.get(rightChar) - 1); | ||
if (tMap.get(rightChar) == 0) { | ||
count--; | ||
} | ||
} | ||
|
||
|
||
while (count == 0) { | ||
if (right - left + 1 < minLen) { | ||
minLen = right - left + 1; | ||
minLeft = left; | ||
} | ||
|
||
char leftChar = s.charAt(left); | ||
|
||
if (tMap.containsKey(leftChar)) { | ||
tMap.put(leftChar, tMap.get(leftChar) + 1); | ||
if (tMap.get(leftChar) > 0) { | ||
count++; | ||
} | ||
} | ||
|
||
left++; | ||
} | ||
} | ||
|
||
return minLen == Integer.MAX_VALUE ? "" : s.substring(minLeft, minLeft + minLen); | ||
} | ||
} |