Java Array and String Operations Guide
Java Array and String Operations Guide
The `LongestUniqueSubstring` program uses a sliding window technique to iterate through the string, maintaining a current substring without repeating characters. It uses a string `current` to track the valid substring and resets it from the character after a duplicate is found. It updates the `longest` substring if the current one is longer. This improves performance by maintaining a continuous substring instead of constructing substrings from scratch repetitively, potentially reducing complexity over a naive O(n^2) solution to a more efficient O(n).
The `SumUpToKey` program sums the elements of the array until it encounters an element that matches the specified key value. If the key is found, the loop breaks, and the sum of elements up to and including the key is printed. If the key value is not present in the array, the program will continue to sum all elements without breaking, resulting in the total sum of the array elements being printed .
The `BinarySearch` algorithm works by repeatedly dividing the search interval in half, comparing the middle element of the array with the target key. If the middle element equals the key, it returns the index; otherwise, it narrows the search to the lower or upper half, depending on whether the key is smaller or larger than the middle element. This method requires the array to be sorted and has a time complexity of O(log n). Its limitation is that it only works with sorted arrays, and if applied to an unsorted array, it may fail to find the key or produce incorrect results .
The `MissingNumber` program calculates the expected sum of the first n natural numbers using the formula n(n+1)/2, where n is the length of the array plus one because one number is missing. It iterates through the array, subtracting each element from this expected sum. The remaining value in the sum variable after the loop completes is the missing number. This method is efficient, operating in O(n) time complexity .
The `BubbleSort` algorithm sorts the array by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. This process is repeated for each element until no more swaps are needed, indicating that the array is sorted. A major drawback of Bubble Sort is its time complexity of O(n^2), making it inefficient on large lists compared to more advanced algorithms such as QuickSort or MergeSort, which operate in O(n log n).
The `AnagramCheck` program checks if two strings are anagrams by converting both strings to character arrays, sorting these arrays, and then comparing them for equality. When the sorted arrays match, the strings are anagrams. Sorting both strings gives the program a time complexity of O(n log n) due to the sorting operation, where n is the length of the strings. The approach efficiently verifies anagram status but has added cost from sorting compared to a more optimal method using frequency counting .
The `PasswordValidation` program checks password validity using several criteria: the password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one special character from the set [@#$%^&+=]. These conditions are checked using regular expressions combined with logical AND operations. If all conditions are met, the password is considered valid. This approach ensures basic standards for password strength and complexity .
The `PrimeNumbers` program checks each number in the array using the `isPrime` method, which determines if a number is prime by checking divisibility from 2 up to the square root of the number. If divisible by any of these, it's not prime. If a number is prime, it's added to the sum, and the count is incremented. The program then prints all primes, their sum, and the count .
The `SecondLargest` program sorts the entire array in ascending order using `Arrays.sort()` and then directly accesses the second last element as the second largest element. This approach is inefficient for large datasets because it sorts the whole array, which has a time complexity of O(n log n), whereas finding the second largest element can be done more efficiently in O(n) time by a single pass through the array to track the two largest elements .
The 'Longest Increasing Subsequence' (LIS) algorithm uses dynamic programming to find the length of the longest subsequence where elements are in increasing order. It initializes an array `lis` where each element is initially set to 1, representing the minimum possible length. It iteratively updates `lis[i]` for each element such that for every j < i, if arr[i] > arr[j], `lis[i]` could be `lis[j] + 1` if it leads to a longer subsequence. After iterating, the maximum value in the lis array is the length of the LIS. This approach has a time complexity of O(n^2) due to the nested iteration .