Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions solution/3900-3999/3940.Limit Occurrences in Sorted Array/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3940.Limit%20Occurrences%20in%20Sorted%20Array/README_EN.md
---

<!-- problem:start -->

# [3940. Limit Occurrences in Sorted Array](https://leetcode.com/problems/limit-occurrences-in-sorted-array)

## Description

<!-- description:start -->

<p>You are given a sorted integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>Return an array such that each distinct element appears at most <code>k</code> times, while preserving the relative order of the elements in <code>nums</code>.</p>

<p>Note: If a distinct element appears at least <code>k</code> times, then it must appear exactly <code>k</code> times in the resulting array.</p>

<p>&nbsp;</p>

<p><strong class="example">Example 1:</strong></p>

<div class="example-block">

<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,2,2,3], k = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">[1,1,2,2,3]</span></p>

<p><strong>Explanation:</strong></p>

<ul>
<li>The element <code>1</code> appears 3 times, so only 2 occurrences are kept.</li>
<li>The element <code>2</code> appears 2 times, so both occurrences are kept.</li>
<li>The element <code>3</code> appears 1 time, so it is kept.</li>
</ul>

<p>Thus, the resulting array is <code>[1,1,2,2,3]</code>.</p>

</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">

<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>

<p><strong>Explanation:</strong></p>

<p>All elements are distinct and already appear at most once, so the array remains unchanged.</p>

</div>

<p>&nbsp;</p>

<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i] <= 100</code></li>
<li><code>nums</code> is sorted in non-decreasing order.</li>
<li><code>1 <= k <= nums.length</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Counting Consecutive Elements

Since the array is sorted, duplicate elements appear consecutively.

We maintain a counter to track occurrences of the current element.

If the occurrence count is less than or equal to <code>k</code>, we add the element to the result list.

The time complexity is <code>O(n)</code>, where <code>n</code> is the length of the array.

The space complexity is <code>O(n)</code>.

<!-- tabs:start -->

#### Java

```java
class Solution {
public int[] limitOccurrences(int[] nums, int k) {

List<Integer> list = new ArrayList<>();

int count = 1;

list.add(nums[0]);

for (int i = 1; i < nums.length; i++) {

if (nums[i] == nums[i - 1]) {
count++;
} else {
count = 1;
}

if (count <= k) {
list.add(nums[i]);
}
}

int[] ans = new int[list.size()];

for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}

return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int[] limitOccurrences(int[] nums, int k) {
List<Integer> list = new ArrayList<>();
int count = 1;
list.add(nums[0]);
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) {
count++;
} else {
count = 1;
}
if (count <= k) {
list.add(nums[i]);
}
}
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
}
129 changes: 129 additions & 0 deletions solution/3900-3999/3941.Password Strength/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3941.Password%20Strength/README_EN.md
---

<!-- problem:start -->

# [3941. Password Strength](https://leetcode.com/problems/password-strength)

## Description

<!-- description:start -->

<p>You are given a string <code>password</code>.</p>

<p>The strength of the password is calculated based on the following rules:</p>

<ul>
<li>1 point for each distinct lowercase letter (<code>'a'</code> to <code>'z'</code>).</li>
<li>2 points for each distinct uppercase letter (<code>'A'</code> to <code>'Z'</code>).</li>
<li>3 points for each distinct digit (<code>'0'</code> to <code>'9'</code>).</li>
<li>5 points for each distinct special character from the set <code>"!@#$"</code>.</li>
</ul>

<p>Each character contributes at most once, even if it appears multiple times.</p>

<p>Return an integer denoting the strength of the password.</p>

<p>&nbsp;</p>

<p><strong class="example">Example 1:</strong></p>

<div class="example-block">

<p><strong>Input:</strong> <span class="example-io">password = "aA1!"</span></p>

<p><strong>Output:</strong> <span class="example-io">11</span></p>

<p><strong>Explanation:</strong></p>

<p>The distinct characters are <code>'a'</code>, <code>'A'</code>, <code>'1'</code> and <code>'!'</code>.</p>

<p>Thus, the strength = 1 + 2 + 3 + 5 = 11.</p>

</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">

<p><strong>Input:</strong> <span class="example-io">password = "bbB11#"</span></p>

<p><strong>Output:</strong> <span class="example-io">11</span></p>

<p><strong>Explanation:</strong></p>

<p>The distinct characters are <code>'b'</code>, <code>'B'</code>, <code>'1'</code> and <code>'#'</code>.</p>

<p>Thus, the strength = 1 + 2 + 3 + 5 = 11.</p>

</div>

<p>&nbsp;</p>

<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 <= password.length <= 10<sup>5</sup></code></li>
<li><code>password</code> consists of lowercase and uppercase English letters, digits, and special characters from <code>"!@#$"</code>.</li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: HashSet

We use a <code>HashSet</code> to store distinct characters.

For each distinct character:
- Add 1 if it is lowercase.
- Add 2 if it is uppercase.
- Add 3 if it is a digit.
- Add 5 if it is one of <code>!@#$</code>.

The time complexity is <code>O(n)</code>, where <code>n</code> is the length of the string.

The space complexity is <code>O(1)</code>.

<!-- tabs:start -->

#### Java

```java
class Solution {
public int passwordStrength(String password) {
Set<Character> set = new HashSet<>();

for (char ch : password.toCharArray()) {
set.add(ch);
}

int ans = 0;

for (char ch : set) {
if (Character.isLowerCase(ch)) {
ans += 1;
} else if (Character.isUpperCase(ch)) {
ans += 2;
} else if (Character.isDigit(ch)) {
ans += 3;
} else {
ans += 5;
}
}

return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
18 changes: 18 additions & 0 deletions solution/3900-3999/3941.Password Strength/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int passwordStrength(String password) {
Set<Character> set = new HashSet<>();
int count = 0;
for(int i=0;i<password.length();i++){
char c = password.charAt(i);
if(set.contains(c)){
continue;
}
set.add(c);
if(c >= 'a' && c <= 'z') count += 1;
else if(c >= 'A' && c <= 'Z') count += 2;
else if(c >= '0' && c <= '9') count+=3;
else if(c == '!' || c == '@' || c == '#' || c == '$') count+=5;
}
return count;
}
}
Loading