Skip to content

Added tasks 3270-3277 #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 4, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package g1601_1700.s1616_split_two_strings_to_make_palindrome;

// #Medium #String #Greedy #Two_Pointers #2022_04_13_Time_4_ms_(89.77%)_Space_43.3_MB_(85.58%)
// #Medium #String #Greedy #Two_Pointers #2024_09_04_Time_2_ms_(100.00%)_Space_45.1_MB_(97.99%)

@SuppressWarnings("java:S2234")
public class Solution {
public boolean checkPalindromeFormation(String a, String b) {
return check(a, b) || check(b, a);
}

private boolean check(String a, String b) {
int i = 0;
int j = b.length() - 1;
while (j > i && a.charAt(i) == b.charAt(j)) {
++i;
--j;
int n = a.length();
int s = 0;
int e = n - 1;
if (isPalindrome(a, b, s, e, true)) {
return true;
} else {
return isPalindrome(b, a, s, e, true);
}
return isPalindrome(a, i, j) || isPalindrome(b, i, j);
}

private boolean isPalindrome(String s, int i, int j) {
while (j > i && s.charAt(i) == s.charAt(j)) {
++i;
--j;
private boolean isPalindrome(String a, String b, int s, int e, boolean check) {
if (s == e) {
return true;
}
while (s < e) {
if (a.charAt(s) != b.charAt(e)) {
return check
&& (isPalindrome(a, a, s, e, false) || isPalindrome(b, b, s, e, false));
}
s++;
e--;
}
return i >= j;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3201_3300.s3270_find_the_key_of_the_numbers;

// #Easy #Math #2024_09_02_Time_0_ms_(100.00%)_Space_40.5_MB_(100.00%)

public class Solution {
public int generateKey(int num1, int num2, int num3) {
int s1 =
Math.min(((num1 / 1000) % 10), Math.min(((num2 / 1000) % 10), ((num3 / 1000) % 10)))
* 1000;
int s2 =
Math.min(((num1 / 100) % 10), Math.min(((num2 / 100) % 10), ((num3 / 100) % 10)))
* 100;
int s3 =
Math.min(((num1 / 10) % 10), Math.min(((num2 / 10) % 10), ((num3 / 10) % 10))) * 10;
int s4 = Math.min((num1 % 10), Math.min((num2 % 10), (num3 % 10)));
return s1 + s2 + s3 + s4;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3270\. Find the Key of the Numbers

Easy

You are given three **positive** integers `num1`, `num2`, and `num3`.

The `key` of `num1`, `num2`, and `num3` is defined as a four-digit number such that:

* Initially, if any number has **less than** four digits, it is padded with **leading zeros**.
* The <code>i<sup>th</sup></code> digit (`1 <= i <= 4`) of the `key` is generated by taking the **smallest** digit among the <code>i<sup>th</sup></code> digits of `num1`, `num2`, and `num3`.

Return the `key` of the three numbers **without** leading zeros (_if any_).

**Example 1:**

**Input:** num1 = 1, num2 = 10, num3 = 1000

**Output:** 0

**Explanation:**

On padding, `num1` becomes `"0001"`, `num2` becomes `"0010"`, and `num3` remains `"1000"`.

* The <code>1<sup>st</sup></code> digit of the `key` is `min(0, 0, 1)`.
* The <code>2<sup>nd</sup></code> digit of the `key` is `min(0, 0, 0)`.
* The <code>3<sup>rd</sup></code> digit of the `key` is `min(0, 1, 0)`.
* The <code>4<sup>th</sup></code> digit of the `key` is `min(1, 0, 0)`.

Hence, the `key` is `"0000"`, i.e. 0.

**Example 2:**

**Input:** num1 = 987, num2 = 879, num3 = 798

**Output:** 777

**Example 3:**

**Input:** num1 = 1, num2 = 2, num3 = 3

**Output:** 1

**Constraints:**

* `1 <= num1, num2, num3 <= 9999`
20 changes: 20 additions & 0 deletions src/main/java/g3201_3300/s3271_hash_divided_string/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3201_3300.s3271_hash_divided_string;

// #Medium #String #Simulation #2024_09_02_Time_2_ms_(100.00%)_Space_44.7_MB_(100.00%)

public class Solution {
public String stringHash(String s, int k) {
var result = new StringBuilder();
int i = 0;
int sum = 0;
while (i < s.length()) {
sum += s.charAt(i) - 'a';
if ((i + 1) % k == 0) {
result.append((char) ('a' + sum % 26));
sum = 0;
}
i++;
}
return result.toString();
}
}
46 changes: 46 additions & 0 deletions src/main/java/g3201_3300/s3271_hash_divided_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3271\. Hash Divided String

Medium

You are given a string `s` of length `n` and an integer `k`, where `n` is a **multiple** of `k`. Your task is to hash the string `s` into a new string called `result`, which has a length of `n / k`.

First, divide `s` into `n / k` **substrings**, each with a length of `k`. Then, initialize `result` as an **empty** string.

For each **substring** in order from the beginning:

* The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0`, `'b' → 1`, ..., `'z' → 25`).
* Calculate the _sum_ of all the **hash values** of the characters in the substring.
* Find the remainder of this sum when divided by 26, which is called `hashedChar`.
* Identify the character in the English lowercase alphabet that corresponds to `hashedChar`.
* Append that character to the end of `result`.

Return `result`.

**Example 1:**

**Input:** s = "abcd", k = 2

**Output:** "bf"

**Explanation:**

First substring: `"ab"`, `0 + 1 = 1`, `1 % 26 = 1`, `result[0] = 'b'`.

Second substring: `"cd"`, `2 + 3 = 5`, `5 % 26 = 5`, `result[1] = 'f'`.

**Example 2:**

**Input:** s = "mxz", k = 3

**Output:** "i"

**Explanation:**

The only substring: `"mxz"`, `12 + 23 + 25 = 60`, `60 % 26 = 8`, `result[0] = 'i'`.

**Constraints:**

* `1 <= k <= 100`
* `k <= s.length <= 1000`
* `s.length` is divisible by `k`.
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package g3201_3300.s3272_find_the_count_of_good_integers;

// #Hard #Hash_Table #Math #Enumeration #Combinatorics
// #2024_09_02_Time_167_ms_(100.00%)_Space_54.5_MB_(100.00%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Solution {
private final List<String> palindromes = new ArrayList<>();

private long factorial(int n) {
long res = 1;
for (int i = 2; i <= n; i++) {
res *= i;
}
return res;
}

private Map<Character, Integer> countDigits(String s) {
Map<Character, Integer> freq = new HashMap<>();
for (char c : s.toCharArray()) {
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
return freq;
}

private long calculatePermutations(Map<Character, Integer> freq, int length) {
long totalPermutations = factorial(length);
for (int count : freq.values()) {
totalPermutations /= factorial(count);
}
return totalPermutations;
}

private long calculateValidPermutations(String s) {
Map<Character, Integer> freq = countDigits(s);
int n = s.length();
long totalPermutations = calculatePermutations(freq, n);
if (freq.getOrDefault('0', 0) > 0) {
freq.put('0', freq.get('0') - 1);
long invalidPermutations = calculatePermutations(freq, n - 1);
totalPermutations -= invalidPermutations;
}
return totalPermutations;
}

private void generatePalindromes(
int f, int r, int k, int lb, int sum, StringBuilder ans, int[] rem) {
if (f > r) {
if (sum == 0) {
palindromes.add(ans.toString());
}
return;
}
for (int i = lb; i <= 9; i++) {
ans.setCharAt(f, (char) ('0' + i));
ans.setCharAt(r, (char) ('0' + i));
int chk = sum;
chk = (chk + rem[f] * i) % k;
if (f != r) {
chk = (chk + rem[r] * i) % k;
}
generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem);
}
}

private List<String> allKPalindromes(int n, int k) {
StringBuilder ans = new StringBuilder(n);
ans.append("0".repeat(Math.max(0, n)));
int[] rem = new int[n];
rem[0] = 1;
for (int i = 1; i < n; i++) {
rem[i] = (rem[i - 1] * 10) % k;
}
palindromes.clear();
generatePalindromes(0, n - 1, k, 1, 0, ans, rem);
return palindromes;
}

public long countGoodIntegers(int n, int k) {
List<String> ans = allKPalindromes(n, k);
Set<String> st = new HashSet<>();
for (String str : ans) {
char[] arr = str.toCharArray();
Arrays.sort(arr);
st.add(new String(arr));
}
List<String> v = new ArrayList<>(st);
long chk = 0;
for (String str : v) {
long cc = calculateValidPermutations(str);
chk += cc;
}
return chk;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3272\. Find the Count of Good Integers

Hard

You are given two **positive** integers `n` and `k`.

An integer `x` is called **k-palindromic** if:

* `x` is a palindrome.
* `x` is divisible by `k`.

An integer is called **good** if its digits can be _rearranged_ to form a **k-palindromic** integer. For example, for `k = 2`, 2020 can be rearranged to form the _k-palindromic_ integer 2002, whereas 1010 cannot be rearranged to form a _k-palindromic_ integer.

Return the count of **good** integers containing `n` digits.

**Note** that _any_ integer must **not** have leading zeros, **neither** before **nor** after rearrangement. For example, 1010 _cannot_ be rearranged to form 101.

**Example 1:**

**Input:** n = 3, k = 5

**Output:** 27

**Explanation:**

_Some_ of the good integers are:

* 551 because it can be rearranged to form 515.
* 525 because it is already k-palindromic.

**Example 2:**

**Input:** n = 1, k = 4

**Output:** 2

**Explanation:**

The two good integers are 4 and 8.

**Example 3:**

**Input:** n = 5, k = 6

**Output:** 2468

**Constraints:**

* `1 <= n <= 10`
* `1 <= k <= 9`
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob;

// #Hard #Array #Sorting #Greedy #2024_09_04_Time_76_ms_(100.00%)_Space_59.5_MB_(61.02%)

import java.util.Arrays;

@SuppressWarnings("java:S1210")
public class Solution {
public long minDamage(int pw, int[] damage, int[] health) {
long res = 0;
long sum = 0;
for (int e : damage) {
sum += e;
}
Pair[] pairs = new Pair[damage.length];
for (int e = 0; e < damage.length; e++) {
pairs[e] = new Pair(damage[e], (health[e] + pw - 1) / pw);
}
Arrays.sort(pairs);
for (Pair pr : pairs) {
res += pr.val * sum;
sum -= pr.key;
}
return res;
}

static class Pair implements Comparable<Pair> {
int key;
int val;

Pair(int key, int val) {
this.key = key;
this.val = val;
}

@Override
public int compareTo(Pair p) {
return val * p.key - key * p.val;
}
}
}
Loading
Loading