Java Recursion and String Array Questions
Java Recursion and String Array Questions
To find the longest substring without repeating characters, employ a sliding window technique. Use a hash set to track characters within the current window. Expand the window by adding characters from the end until a duplicate is encountered. At that point, incrementally contract the window from the start until the duplicate is removed. Track the maximum length encountered during this dynamic window adjustment .
The recursive process for printing numbers from 1 to N involves decrementing the number N in each recursive call and printing it after the call returns. Specifically, the base case checks if N is zero, upon which the recursion stops. Otherwise, the function calls itself with N-1 and prints the current value of N after the recursive call returns, resulting in numbers 1 up to N being printed in order .
To determine if a string is a palindrome using recursion, compare the first and last characters of the string. If they are identical, the function recursively checks the substring that excludes these characters. The base case is reached when the pointers meet or cross each other, indicating that the entire string has been verified as symmetrical. If any characters do not match, the function immediately returns false .
To compute the sum of digits of a number using recursion, you repeatedly break down the number into its last digit and the remaining number. The last digit is found using the modulus operation (n % 10), and the remaining is obtained by integer division (n / 10). The base case for this recursion is when the number becomes zero, at which point the function returns 0, terminating the recursion .
To determine if one string is a rotation of another, first check if they are of equal length. Concatenate the first string with itself and check if the second string is a substring of this concatenated result. This operation effectively accounts for all possible rotations. If the second string is found within, it confirms that the strings are rotations of each other .
The algorithm for finding the first non-repeated character in a string uses a LinkedHashMap to map each character to its frequency in the string. The LinkedHashMap preserves the order of insertion, enabling sequential scanning. Each character of the string is iterated, updating the frequency map. A second traversal checks for the first character with a frequency of one, identifying it as the first non-repeating character .
The approach to find the missing number in an array of distinct numbers from 1 to n is to compute the theoretical sum of numbers from 1 to n using the formula n*(n+1)/2. Then calculate the actual sum of the numbers present in the array. The difference between the theoretical sum and the actual sum yields the missing number. This leverages the properties of arithmetic sequences and eliminates the need for additional data structures .
The recursive solution to the Tower of Hanoi problem involves strategically moving the smallest (n-1) number of disks to an auxiliary peg, then moving the largest disk directly to the destination peg. Subsequently, the (n-1) disks that were moved to the auxiliary peg are moved to the destination peg on top of the largest disk. This recursive strategy breaks down the problem into smaller subsets until it is trivial enough to solve directly, with the base case involved when only a single disk needs to be moved .
The recursive approach to reversing a string involves decomposing the string into its first character and the remaining substring. The function calls itself with the remaining substring and appends the first character to the result of the recursive call. The base case occurs when the substring is empty, at which point it returns the empty string, allowing the characters to be concatenated in reverse order .
To verify if two strings are anagrams using Java, first remove any spaces and convert both strings to lowercase. Convert the strings into character arrays and sort them. Lastly, compare the sorted arrays; if they are identical, the original strings are anagrams. This technique effectively neutralizes any variation in character order, focusing on composition .