Logo
TGCATALOG
Catalog Collections Blog
Kotlin | LeetCode

Kotlin | LeetCode

LeetCode problems and solutions in Kotlin.

No ratings
430
01.12.2026
430
01.12.2026
No ratings
430
01.12.2026
Safe redirect via bot
О канале

LeetCode problems and solutions in Kotlin.

Подписчиков —
Тематика Technology
Ссылка @easy_kotlin_task

We also recommend

Kekaton AI | Voiceover and voice clone
Kekaton AI | Voiceover and voice clone
Bot

AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.

Description

LeetCode problem solutions in Kotlin. Optimized code for mobile developers and algorithm specialists.

Latest posts

Kotlin | LeetCode
Kotlin | LeetCode
🔒Открыть пост
и посмотреть медиа
Задача: 859. Buddy Strings Сложность: easy Даны две строки s и goal. Верните true, если вы можете поменять местами две буквы в s так, чтобы результат был равен goal, в противном случае верните false. Обмен буквами определяется как взятие двух индексов i и j (нумерация с 0), таких что i != j, и обмен символов в s[i] и s[j]. Например, обмен символов на индексах 0 и 2 в строке "abcd" приводит к "cbad". Пример: Input: s = "ab", goal = "ba" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal. 👨💻 Алгоритм: 1⃣Если количество символов в строках s и goal разное, возвращаем false. Если s == goal, используем хеш-таблицу или массив из 26 элементов для хранения частоты каждого символа в строке s. Если какой-либо символ встречается более одного раза, можно поменять местами две одинаковые буквы, возвращаем true. Иначе возвращаем false. 2⃣Иначе, если s != goal, инициализируем firstIndex и secondIndex значениями -1 для хранения индексов символов в строке s, которые отличаются от символов в строке goal на тех же индексах. Итерируем по каждому индексу i в строке s: если символы s[i] и goal[i] разные, сохраняем текущий индекс. Если firstIndex == -1, обновляем firstIndex = i. Если firstIndex != -1, но secondIndex == -1, обновляем secondIndex = i. Если оба индекса уже обновлены, возвращаем false. 3⃣Если обновлен только firstIndex, возвращаем false. Иначе, все символы обеих строк одинаковы, кроме двух индексов. Поэтому s[firstIndex] должен быть равен goal[secondIndex], и s[secondIndex] должен быть равен goal[firstIndex], чтобы строки стали равны после обмена. 😎 Решение: class Solution { fun buddyStrings(s: String, goal: String): Boolean { if (s.length != goal.length) return false if (s == goal) { val freq = IntArray(26) for (ch in s) { if (++freq[ch - 'a'] > 1) return true } return false } var firstIndex = -1 var secondIndex = -1 for (i in s.indices) { if (s[i] != goal[i]) { if (firstIndex == -1) firstIndex = i else if (secondIndex == -1) secondIndex = i else return false } } return secondIndex != -1 && s[firstIndex] == goal[secondIndex] && s[secondIndex] == goal[firstIndex] } } Ставь 👍 и забирай 📚 Базу знаний
Kotlin | LeetCode
FAQ

Open Kotlin | LeetCode in Telegram and press the Start button. No separate registration is required.

Yes. The service is designed to be straightforward: open the resource, follow the on-screen instructions and start using it.

No. You can use Kotlin | LeetCode directly in Telegram unless the resource description says otherwise.

Access depends on the resource. Check the current information in Telegram after opening Kotlin | LeetCode.

The exact features depend on the resource. Open Kotlin | LeetCode to see its current functions, content and access conditions.

Reviews for channel Kotlin | LeetCode

Log in to leave a review

Only registered users can share their opinion.

No reviews yet

Be the first to share your impression of this resource!

Similar resources

Loading
Switch to Light Theme
Home Catalog Collections Blog Login