Skip to content

Commit

Permalink
solution(java): Problems 76 and 233
Browse files Browse the repository at this point in the history
76. Minimum Window Substring
- Java

233. Number of Digit One
- Java
  • Loading branch information
godkingjay authored Oct 31, 2023
2 parents 783c9c8 + f11b2b9 commit 5183fdb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Hard/233. Number of Digit One/README.md
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/
12 changes: 12 additions & 0 deletions Hard/233. Number of Digit One/Sol.java
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;
}
}
39 changes: 39 additions & 0 deletions Hard/76.Minimum Window Substring/README.md
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/
52 changes: 52 additions & 0 deletions Hard/76.Minimum Window Substring/Sol1.java
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);
}
}

0 comments on commit 5183fdb

Please sign in to comment.