Tag Archives: leetcode çözümleri

LeetCode Çözümleri – 1185. Day of the Week [Zeller’s congruence]

LeetCode içerisinde bulunan “Day of the Week” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen gün, ay ve yıl bilgilerine ait tarihin haftanın hangi güne denk geldiğini bulmanız isteniyor.

► LeetCode 1185. Day of the Week: https://leetcode.com/problems/day-of-the-week/

► Zeller’s congruence https://en.wikipedia.org/wiki/Zeller%27s_congruence

► Problem açıklaması:

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}.

Example 1: Input: day = 31, month = 8, year = 2019 Output: “Saturday”

Example 2: Input: day = 18, month = 7, year = 1999 Output: “Sunday”

Example 3: Input: day = 15, month = 8, year = 1993 Output: “Sunday”

Constraints: The given dates are valid dates between the years 1971 and 2100.

LeetCode Çözümleri – 796. Rotate String

LeetCode içerisinde bulunan “Rotate String” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen iki string‘ten, ilk string herhangi bir sayıda rotasyon ile ikinci string’e dönüşüp dönüşemeyeceği soruluyor.

► LeetCode 796. Rotate String: https://leetcode.com/problems/rotate-string/

► Problem açıklaması:

We are given two strings, s and goal.

A shift on s consists of taking string s and moving the leftmost character to the rightmost position. For example, if s = ‘abcde’, then it will be ‘bcdea’ after one shift on s. Return true if and only if s can become goal after some number of shifts on s.

Example 1:

Input: s = ‘abcde’, goal = ‘cdeab’

Output: true

Example 2:

Input: s = ‘abcde’, goal = ‘abced’

Output: false

Note:

s and goal will have length at most 100.

LeetCode Çözümleri – 1544. Make The String Great

LeetCode içerisinde bulunan “Make The String Great” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string‘i, “iyi” bir string yapmanız gerekiyor. Bu da string’i, birbirini takip eden aynı karakterlerden – fakat biri küçük diğer büyük harf olacak şekilde – arındırmanız demek.

► LeetCode 1544. Make The String Great: https://leetcode.com/problems/make-the-string-great/

► Problem açıklaması:

Given a string s of lower and upper case English letters.

A good string is a string which doesn’t have two adjacent characters s[i] and s[i + 1] where:

0 <= i <= s.length – 2

s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

Example 1:

Input: s = “leEeetcode”

Output: “leetcode”

Explanation: In the first step, either you choose i = 1 or i = 2, both will result “leEeetcode” to be reduced to “leetcode”.

Example 2:

Input: s = “abBAcC”

Output: “”

Explanation: We have many possible scenarios, and all lead to the same answer. For example:

“abBAcC” — “aAcC” — “cC” — “”

“abBAcC” — “abBA” — “aA” — “”

Example 3:

Input: s = “s”

Output: “s”

LeetCode Çözümleri – 1137. N-th Tribonacci Number

LeetCode içerisinde bulunan “N-th Tribonacci Number” sorusunun açıklaması ve çözümü. Bu soruda Tribonacci serisinin n. elemanını bulmanız isteniyor.

► LeetCode 1137. N-th Tribonacci Number: https://leetcode.com/problems/n-th-tribonacci-number/

► Problem açıklaması:

The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

Example 1:

Input: n = 4

Output: 4

Explanation:

T_3 = 0 + 1 + 1 = 2

T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25

Output: 1389537

Constraints:

0 <= n <= 37

The answer is guaranteed to fit within a 32-bit integer, ie. answer lessEqual 2^31 – 1.

LeetCode Çözümleri – 459. Repeated Substring Pattern

LeetCode içerisinde bulunan “Repeated Substring Pattern” sorusunun açıklaması ve çözümü. Bu soruda size verilen string‘in belirli bir string’in kendini tekrar etmesi ile oluşup oluşmadığı soruluyor.

► LeetCode 459. Repeated Substring Pattern: https://leetcode.com/problems/repeated-substring-pattern/

► Problem açıklaması:

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Example 1:

Input: s = “abab”

Output: true

Explanation: It is the substring “ab” twice.

Example 2:

Input: s = “aba”

Output: false

Example 3:

Input: s = “abcabcabcabc”

Output: true

Explanation: It is the substring “abc” four times or the substring “abcabc” twice.

Constraints:

1 <= s.length <= 10^4

s consists of lowercase English letters.

LeetCode Çözümleri – 1154. Day of the Year

LeetCode içerisinde bulunan “Day of the Year” sorusunun açıklaması ve çözümü. Bu soruda size verilen string‘in gösterdiği tarihin yılın kaçıncı günü olduğunu bulmanız isteniyor.

► LeetCode 1154. Day of the Year: https://leetcode.com/problems/day-of-the-year/

► Problem açıklaması: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

Example 1:

Input: date = “2019-01-09”

Output: 9

Explanation: Given date is the 9th day of the year in 2019.

Example 2: Input: date = “2019-02-10” Output: 41

Example 3: Input: date = “2003-03-01” Output: 60

Example 4: Input: date = “2004-03-01” Output: 61

Constraints:

date.length == 10

date[4] == date[7] == ‘-‘, and all other date[i]’s are digits

date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

LeetCode Çözümleri – 485. Max Consecutive Ones

LeetCode içerisinde bulunan “Max Consecutive Ones” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde (1 ve 0’lardan oluşan) yan yana olan 1’lerin en uzun hali soruluyor.

► LeetCode 228. 485. Max Consecutive Ones: https://leetcode.com/problems/max-consecutive-ones/

► Problem açıklaması:

Given a binary array nums, return the maximum number of consecutive 1’s in the array.

Example 1:

Input: nums = [1,1,0,1,1,1]

Output: 3

Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

Example 2:

Input: nums = [1,0,1,1,0,1]

Output: 2

Constraints:

1 <= nums.length <= 10^5

nums[i] is either 0 or 1.

LeetCode Çözümleri – 448. Find All Numbers Disappeared in an Array

LeetCode içerisinde bulunan “Find All Numbers Disappeared in an Array” sorusunun açıklaması ve çözümü. Bu soruda size [1, n] aralığındaki sayıları içerebilen bir tam sayı dizisinde, [1, n] aralığında olup ta bu dizide olmayan sayıları geriye döndürmeniz isteniyor.

► LeetCode 228. 448. Find All Numbers Disappeared in an Array: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

► Problem açıklaması:

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]

Output: [5,6]

Example 2:

Input: nums = [1,1]

Output: [2]

Constraints:

n == nums.length

1 <= n <= 10^5

1 <= nums[i] <= n

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

LeetCode Çözümleri – 228. Summary Ranges

LeetCode içerisinde bulunan “Summary Ranges” sorusunun açıklaması ve çözümü. Bu soruda size verilen tam sayılarda, birbirini takip edenleri “aralık” olarak yazdırmanız isteniyor. Yani [0,1,2,4,5,7] için sizden [“0–2″,”4–5″,”7”] şeklinde bir liste isteniyor.

► LeetCode 228. Summary Ranges: https://leetcode.com/problems/summary-ranges/

► Problem açıklaması:

You are given a sorted unique integer array nums.

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

“a–b” if a != b

“a” if a == b

Example 1:

Input: nums = [0,1,2,4,5,7]

Output: [“0–2″,”4–5″,”7”]

Explanation: The ranges are:

[0,2] — “0–2”

[4,5] — “4–5”

[7,7] — “7”

Example 2:

Input: nums = [0,2,3,4,6,8,9]

Output: [“0″,”2–4″,”6″,”8–9”]

Explanation: The ranges are:

[0,0] — “0”

[2,4] — “2–4”

[6,6] — “6”

[8,9] — “8–9”

Example 3:

Input: nums = []

Output: []

Example 4:

Input: nums = [-1]

Output: [“-1”]

Example 5:

Input: nums = [0]

Output: [“0”]

Constraints:

0 lessEqual nums.length lessEqual 20

-2^31 <= nums[i] <= 2^31 – 1

All the values of nums are unique.

nums is sorted in ascending order.

LeetCode Çözümleri – 1184. Distance Between Bus Stops

LeetCode içerisinde bulunan “Distance Between Bus Stops” sorusunun açıklaması ve çözümü. Bu soruda size verilen başlangıç ve bitiş otobüs durak numaralarına göre (ki bu duraklar dairesel olarak dizilmişler), bunlar arasında gidebileceğiniz en kısa mesafeyi (saat yönünde veya tersinde) bulmanız isteniyor.

► LeetCode 1184. Distance Between Bus Stops: https://leetcode.com/problems/distance-between-bus-stops/

► Problem açıklaması:

A bus has n stops numbered from 0 to n – 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

The bus goes along both directions i.e. clockwise and counterclockwise. Return the shortest distance between the given start and destination stops.

Example 1:

Input: distance = [1,2,3,4], start = 0, destination = 1

Output: 1

Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1

Example 2:

Input: distance = [1,2,3,4], start = 0, destination = 2

Output: 3

Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.

Example 3:

Input: distance = [1,2,3,4], start = 0, destination = 3

Output: 4

Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.

Constraints:

1 <= n <= 10^4

distance.length == n

0 <= start, destination < n

0 <= distance[i] <= 10^4