DSA

Data structure & algorithm using swift language.

Odd Even Linked List

[Question] Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. You must solve the problem in O(1) extra space complexity and O(n) time complexity. Example 1: Input: head = [1,2,3,4,5] Output: [1,3,5,2,4]

Odd Even Linked List Read More »

Count number of substrings with exactly k distinct characters

[Question]: You are given a string ‘str’ of lowercase alphabets and an integer’k’ .Your task is to return the count all the possible substrings that have exactly ‘k’ distinct characters.For example:‘str’ = abcad and ‘k’ = 2.We can see that the substrings {ab, bc, ca, ad} are the only substrings with 2 distinct characters. Therefore,

Count number of substrings with exactly k distinct characters Read More »