From 639dde4138aed4a7ad819579f508dee91dc0669e Mon Sep 17 00:00:00 2001 From: Kanam Ramu Date: Sun, 24 May 2026 15:39:08 +0530 Subject: [PATCH 1/5] Create README for Limit Occurrences in Sorted Array Added README for problem 3940, including problem description, examples, and a solution in Java. --- .../READMEEN.md | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md diff --git a/solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md b/solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md new file mode 100644 index 0000000000000..13846d9b9864a --- /dev/null +++ b/solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md @@ -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 +--- + + + +# [3940. Limit Occurrences in Sorted Array](https://leetcode.com/problems/limit-occurrences-in-sorted-array) + +## Description + + + +

You are given a sorted integer array nums and an integer k.

+ +

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

+ +

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

+ +

 

+ +

Example 1:

+ +
+ +

Input: nums = [1,1,1,2,2,3], k = 2

+ +

Output: [1,1,2,2,3]

+ +

Explanation:

+ + + +

Thus, the resulting array is [1,1,2,2,3].

+ +
+ +

Example 2:

+ +
+ +

Input: nums = [1,2,3], k = 1

+ +

Output: [1,2,3]

+ +

Explanation:

+ +

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

+ +
+ +

 

+ +

Constraints:

+ + + + + +## Solutions + + + +### 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 k, we add the element to the result list. + +The time complexity is O(n), where n is the length of the array. + +The space complexity is O(n). + + + +#### Java + +```java +class Solution { + public int[] limitOccurrences(int[] nums, int k) { + + List 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; + } +} +``` + + + + + + From 190638cab66edf17152fb399209a40b4389c49f8 Mon Sep 17 00:00:00 2001 From: Kanam Ramu Date: Sun, 24 May 2026 15:49:55 +0530 Subject: [PATCH 2/5] Rename READMEEN.md to README_EN.md --- .../{READMEEN.md => README_EN.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/3900-3999/3940.Limit Occurrences in Sorted Array/{READMEEN.md => README_EN.md} (100%) diff --git a/solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md b/solution/3900-3999/3940.Limit Occurrences in Sorted Array/README_EN.md similarity index 100% rename from solution/3900-3999/3940.Limit Occurrences in Sorted Array/READMEEN.md rename to solution/3900-3999/3940.Limit Occurrences in Sorted Array/README_EN.md From 94aaecd8a833003f9820ae007dd5a9c1e6e5405b Mon Sep 17 00:00:00 2001 From: Kanam Ramu Date: Sun, 24 May 2026 15:50:51 +0530 Subject: [PATCH 3/5] Implement limitOccurrences method to restrict duplicates --- .../Solution.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/3900-3999/3940.Limit Occurrences in Sorted Array/Solution.java diff --git a/solution/3900-3999/3940.Limit Occurrences in Sorted Array/Solution.java b/solution/3900-3999/3940.Limit Occurrences in Sorted Array/Solution.java new file mode 100644 index 0000000000000..122229bcbbd03 --- /dev/null +++ b/solution/3900-3999/3940.Limit Occurrences in Sorted Array/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int[] limitOccurrences(int[] nums, int k) { + List 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; + } +} From d2beb1f011bb68742471b4801a2931ccdda405cd Mon Sep 17 00:00:00 2001 From: Kanam Ramu Date: Sun, 24 May 2026 15:58:35 +0530 Subject: [PATCH 4/5] Create README for 3941. Password Strength Added README_EN.md for Password Strength problem with description, examples, and solution. --- .../3941.Password Strength/README_EN.md | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 solution/3900-3999/3941.Password Strength/README_EN.md diff --git a/solution/3900-3999/3941.Password Strength/README_EN.md b/solution/3900-3999/3941.Password Strength/README_EN.md new file mode 100644 index 0000000000000..59f4a85971584 --- /dev/null +++ b/solution/3900-3999/3941.Password Strength/README_EN.md @@ -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 +--- + + + +# [3941. Password Strength](https://leetcode.com/problems/password-strength) + +## Description + + + +

You are given a string password.

+ +

The strength of the password is calculated based on the following rules:

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

Each character contributes at most once, even if it appears multiple times.

+ +

Return an integer denoting the strength of the password.

+ +

 

+ +

Example 1:

+ +
+ +

Input: password = "aA1!"

+ +

Output: 11

+ +

Explanation:

+ +

The distinct characters are 'a', 'A', '1' and '!'.

+ +

Thus, the strength = 1 + 2 + 3 + 5 = 11.

+ +
+ +

Example 2:

+ +
+ +

Input: password = "bbB11#"

+ +

Output: 11

+ +

Explanation:

+ +

The distinct characters are 'b', 'B', '1' and '#'.

+ +

Thus, the strength = 1 + 2 + 3 + 5 = 11.

+ +
+ +

 

+ +

Constraints:

+ +
    +
  • 1 <= password.length <= 105
  • +
  • password consists of lowercase and uppercase English letters, digits, and special characters from "!@#$".
  • +
+ + + +## Solutions + + + +### Solution 1: HashSet + +We use a HashSet 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 !@#$. + +The time complexity is O(n), where n is the length of the string. + +The space complexity is O(1). + + + +#### Java + +```java +class Solution { + public int passwordStrength(String password) { + Set 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; + } +} +``` + + + + + + From 9e55b67381fc76cbf90fa5bf25f69d8dfe8fe4f2 Mon Sep 17 00:00:00 2001 From: Kanam Ramu Date: Sun, 24 May 2026 15:59:07 +0530 Subject: [PATCH 5/5] Implement password strength evaluation method --- .../3941.Password Strength/Solution.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 solution/3900-3999/3941.Password Strength/Solution.java diff --git a/solution/3900-3999/3941.Password Strength/Solution.java b/solution/3900-3999/3941.Password Strength/Solution.java new file mode 100644 index 0000000000000..655a8c62d51ea --- /dev/null +++ b/solution/3900-3999/3941.Password Strength/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int passwordStrength(String password) { + Set set = new HashSet<>(); + int count = 0; + for(int i=0;i= '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; + } +}