DEV Community

Karthika Movva
Karthika Movva

Posted on

My Problem Solving Experience

Hi, folks! Today, I solved three problems on LeetCode: “Find All Anagrams in a String,” “Longest Consecutive Sequence,” and “Search in Rotated Sorted Array.” These problems are really interesting, and we have different logical approaches to solve them. They are extensions of the classic problems of checking if two strings are anagrams and searching for a target element in an array.

Find All Anagrams in a String This problem can be solved by the sliding window technique. We should scan the input array while keeping track of a fixed length of consecutive elements. We check whether this segment is an anagram of the target string. If it is, we add the index to the result array; if not, we ignore the index. In this way we can solve the problem.

Longest Consecutive Sequence: To solve this problem we first eliminate duplicate elements using a set. We traverse the array and check if there exists any sequence of elements that are consecutive in nature (+1 or -1). If it so exists then we keep a track of its length, otherwise, we simply ignore this. This way we can find out the length of the longest consecutive sequence.

Search in Rotated Sorted Array: We can solve this problem using the binary search approach. We divide the input array into two halves. We then determine that which half is sorted, and then do a binary search for that half to find our target element. If the target was not in the sorted half, we continue the search for the target element in the unsorted half. If the target element is not found in either of the halves, then we return -1. In this way, we can solve this problem.

Top comments (0)