All posts by Soner Gönül

LeetCode Çözümleri – 605. Can Place Flowers

LeetCode içerisinde bulunan “Can Place Flowers” sorusunun açıklaması ve çözümü. Bu soruda bir çiçekliği temsil bir tam sayı dizisinde, iki çiçeğin yan yana olmaması koşuluyla verilen çiçek sayısının hepsinin yerleştirilip yerleştirilemeyeceği soruluyor.

► LeetCode 605. Can Place Flowers: https://leetcode.com/problems/can-place-flowers/

► Problem açıklaması:

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0’s and 1’s, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1

Output: true

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2

Output: false

Constraints:

1 <= flowerbed.length <= 2 * 10^4

flowerbed[i] is 0 or 1.

There are no two adjacent flowers in flowerbed.

0 <= n <= flowerbed.length

HackerRank Çözümleri – Mars Exploration

HackerRank içerisinde bulunan “Mars Exploration” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “SOS” string’inin orijinalinden kaç adet farklı karakter barındırdığını bulmanız isteniyor.

► HackerRank – Mars Exploration: https://www.hackerrank.com/challenges/mars-exploration/problem

► Problem açıklaması:

A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help.

Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of the SOS message have been changed by radiation.

Example

s = “SOSTOT”

The original message was SOSSOS. Two of the message’s characters were changed in transit.

Function Description

Complete the marsExploration function in the editor below.

marsExploration has the following parameter(s):

string s: the string as received on Earth

Returns

int: the number of letters changed during transmission

Sample Input 0

SOSSPSSQSSOR

Sample Output 0

3

Explanation 0

= SOSSPSSQSSOR, and signal length s = |12|. They sent 4 SOS messages (i.e.: ).

Expected signal: SOSSOSSOSSOS

Recieved signal: SOSSPSSQSSOR

Difference: X X X

Sample Input 1

SOSSOT

Sample Output 1

1

Explanation 1

s = SOSSOT, and signal length 6. They sent 2 SOS messages (i.e.: ).

Expected Signal: SOSSOS

Received Signal: SOSSOT

Difference: X

Sample Input 2

SOSSOSSOS

Sample Output 2

0

Explanation 2

Since no character is altered, return 0

LeetCode Çözümleri – 504. Base 7

LeetCode içerisinde bulunan “Base 7” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit integer değerin 7 tabanında gösterimini geriye döndürmeniz isteniyor.

► LeetCode 504. Base 7: https://leetcode.com/problems/base-7/

► Problem açıklaması:

Given an integer num, return a string of its base 7 representation.

Example 1:

Input: num = 100

Output: “202”

Example 2:

Input: num = -7

Output: “-10”

Constraints:

-10^7 lessEqual num lessEqual 10^7

HackerRank Çözümleri – Super Reduced String

HackerRank içerisinde bulunan “Super Reduced String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string‘in içerisinde, yan yana olan aynı karakterlerin silinerek elde edilebilecek son string’i bulmanız isteniyor.

► HackerRank – Super Reduced String: https://www.hackerrank.com/challenges/reduced-string/problem

► Problem açıklaması:

Reduce a string of lowercase characters in range ascii[‘a’..’z’]by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them.

Delete as many characters as possible using this method and return the resulting string. If the final string is empty, return Empty String

Example.

s = “aab”

aab shortens to b in one operation: remove the adjacent a characters.

s = “abba”

Remove the two ‘b’ characters leaving ‘aa’. Remove the two ‘a’ characters to leave ”. Return ‘Empty String’.

Function Description

Complete the superReducedString function in the editor below.

superReducedString has the following parameter(s):

string s: a string to reduce

Returns

string: the reduced string or Empty String

Sample Input 0

aaabccddd

Sample Output 0

abd

Explanation 0

Perform the following sequence of operations to get the final string:

aaabccddd → abccddd → abddd → abd

Sample Input 1

aa

Sample Output 1

Empty String

Explanation 1 aa → Empty String

Sample Input 2

baab

Sample Output 2

Empty String

Explanation 2

baab → bb → Empty String

LeetCode Çözümleri – 345. Reverse Vowels of a String

LeetCode içerisinde bulunan “Reverse Vowels of a String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string içerisindeki ünlü harfleri tersten yazdırmanız isteniyor.

► LeetCode 345. Reverse Vowels of a String: https://leetcode.com/problems/reverse-vowels-of-a-string/

► Problem açıklaması:

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.

Example 1:

Input: s = “hello”

Output: “holle”

Example 2:

Input: s = “leetcode”

Output: “leotcede”

Constraints:

1 <= s.length <= 3 * 105

s consist of printable ASCII characters.

HackerRank Çözümleri – Grading Students

HackerRank içerisinde bulunan “Grading Students” sorusunun açıklaması ve çözümü. Bu soruda size verilen sınav sonuçlarını, aşağıda yazılan kurallara göre yuvarlamanız isteniyor.

► HackerRank – Grading Students: https://www.hackerrank.com/challenges/grading/problem

► Problem açıklaması:

HackerLand University has the following grading policy:

– Every student receives a in the inclusive range from 0 to 100.

– Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student’s according to these rules:

– If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.

– If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

Examples

grade = 84 round to (85 – 84 is less than 3)

grade = 29 do not round (result is less than 40)

grade = 57 do not round (60 – 57 is 3 or higher)

Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below.

gradingStudents has the following parameter(s):

int grades[n]: the grades before rounding

Returns

int[n]: the grades after rounding as appropriate

Sample Input 0

4

73

67

38

33

Sample Output 0

75

67

40

33

HackerRank Çözümleri – CamelCase

HackerRank içerisinde bulunan “CamelCase” sorusunun açıklaması ve çözümü. Bu soruda size camelCase olarak yazılmış bir string’in içerisinde kaç adet kelime olduğunu bulmanız isteniyor.

► HackerRank – CamelCase: https://www.hackerrank.com/challenges/camelcase/problem

► Problem açıklaması:

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

– It is a concatenation of one or more words consisting of English letters.

– All letters in the first word are lowercase.

– For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

Example

s = oneTwoThree

There are 3 words in the string: ‘one’, ‘Two’, ‘Three’.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

string s: the string to analyze

Returns

int: the number of words in s.

Sample Input

saveChangesInTheEditor

Sample Output

5

Explanation

String s contains five words:

1. save

2. Changes

3. In

4. The

5. Editor

LeetCode Çözümleri – 500. Keyboard Row

LeetCode içerisinde bulunan “Keyboard Row” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string dizisi içerisinde, Amerikan klavyesi üzerinde aynı satırdaki harflerden oluşan string’leri geri döndürmeniz isteniyor.

► LeetCode 500. Keyboard Row: https://leetcode.com/problems/keyboard-row/

► Problem açıklaması:

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

In the American keyboard:

the first row consists of the characters “qwertyuiop”,

the second row consists of the characters “asdfghjkl”, and

the third row consists of the characters “zxcvbnm”.

Example 1:

Input: words = [“Hello”,”Alaska”,”Dad”,”Peace”]

Output: [“Alaska”,”Dad”]

Example 2:

Input: words = [“omk”]

Output: []

Example 3:

Input: words = [“adsdf”,”sfd”]

Output: [“adsdf”,”sfd”]

Constraints: 1 <= words.length <= 20

1 <= words[i].length <= 100

words[i] consists of English letters (both lowercase and uppercase).

HackerRank Çözümleri – Fibonacci Modified

HackerRank içerisinde bulunan “Fibonacci Modified” sorusunun açıklaması ve çözümü. Bu soruda fibonacci serisinin değiştirilmiş hali olarak, bir önceki sayının kendisi değil de karesinin toplamı şeklinde çözüm bulmanız isteniyor.

► HackerRank – Fibonacci Modified: https://www.hackerrank.com/challenges/fibonacci-modified/problem

► Problem açıklaması:

Implement a modified Fibonacci sequence using the following definition:

t(i + 2) = t(i) + t(i + 2)^2

Given three integers, t1, t2, and n, compute and print the nth term of a modified Fibonacci sequence.

Function Description

Complete the fibonacciModified function in the editor below. It must return the nth number in the sequence.

fibonacciModified has the following parameter(s):

int t1: an integer

int t2: an integer

int n: the iteration to report

Returns

int: the nth number in the sequence

Note: The value of t[n] may far exceed the range of a 64-bit integer. Many submission languages have libraries that can handle such large results but, for those that don’t (e.g., C++), you will need to compensate for the size of the result.

Sample Input

0 1 5

Sample Output

5

LeetCode Çözümleri – 342. Power of Four

LeetCode içerisinde bulunan “Power of Four” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit signed bir tam sayının 4’ün tam kuvveti olup olmadığını bulmanız isteniyor.

► LeetCode 342. Power of Four: https://leetcode.com/problems/power-of-four/

► Problem açıklaması:

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4^x.

Example 1:

Input: n = 16

Output: true

Example 2:

Input: n = 5

Output: false

Example 3:

Input: n = 1

Output: true

Constraints: -2^31 lessEqual n lessEqual 2^31 – 1