The document contains a collection of Java methods for various string and array manipulations, including reversing strings, checking for palindromes, finding anagrams, and more. Each method is implemented with clear logic and utilizes Java's built-in data structures and libraries. The methods cover a wide range of functionality, from basic operations to more complex algorithms like finding the Kth largest element and top K frequent elements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views8 pages
Java8 Problems Solutions All 50
The document contains a collection of Java methods for various string and array manipulations, including reversing strings, checking for palindromes, finding anagrams, and more. Each method is implemented with clear logic and utilizes Java's built-in data structures and libraries. The methods cover a wide range of functionality, from basic operations to more complex algorithms like finding the Kth largest element and top K frequent elements.
List<String> substrings = new ArrayList<>(); for (int i = 0; i < str.length(); i++) { for (int j = i + 1; j <= str.length(); j++) { substrings.add(str.substring(i, j)); } } return substrings; }
17. Group Anagrams
public List<List<String>> groupAnagrams(String[] strs) {
return new ArrayList<>(Arrays.stream(strs) .collect(Collectors.groupingBy(s -> { char[] chars = s.toCharArray(); Arrays.sort(chars); return new String(chars); })).values()); }
18. Valid Parentheses
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '(') stack.push(')'); else if (c == '{') stack.push('}'); else if (c == '[') stack.push(']'); else if (stack.isEmpty() || stack.pop() != c) return false; } return stack.isEmpty(); }
19. Longest Common Prefix
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; }
20. Implement strStr()
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle); } 21. Integer to Roman
public String intToRoman(int num) {
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < values.length && num > 0; i++) { while (num >= values[i]) { num -= values[i]; sb.append(symbols[i]); } } return sb.toString(); }
22. Roman to Integer
public int romanToInt(String s) {
Map<Character, Integer> map = Map.of('I', 1, 'V', 5, 'X', 10, 'L', 50, 'C', 100, 'D', 500, 'M', 1000); int total = 0; for (int i = 0; i < s.length(); i++) { int val = map.get(s.charAt(i)); if (i + 1 < s.length() && val < map.get(s.charAt(i + 1))) { total -= val; } else { total += val; } } return total; }
23. Array Rotation
public void rotate(int[] nums, int k) {
k = k % nums.length; reverse(nums, 0, nums.length - 1); reverse(nums, 0, k - 1); reverse(nums, k, nums.length - 1); } private void reverse(int[] nums, int start, int end) { while (start < end) { int temp = nums[start]; nums[start++] = nums[end]; nums[end--] = temp; } }
24. Find the Missing Number
public int missingNumber(int[] nums) { int n = nums.length; int expectedSum = n * (n + 1) / 2; int actualSum = Arrays.stream(nums).sum(); return expectedSum - actualSum; }
25. Find the Duplicate Number
public int findDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>(); for (int num : nums) { if (!seen.add(num)) return num; } return -1; }
26. Two Sum
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[]{map.get(complement), i}; } map.put(nums[i], i); } return new int[0]; }
27. Three Sum
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { res.add(Arrays.asList(nums[i], nums[left], nums[right])); while (left < right && nums[left] == nums[left + 1]) left++; while (left < right && nums[right] == nums[right - 1]) right--; left++; right--; } else if (sum < 0) left++; else right--; } } return res; }
28. Maximum Subarray
public int maxSubArray(int[] nums) {
int max = nums[0], curr = nums[0]; for (int i = 1; i < nums.length; i++) { curr = Math.max(nums[i], curr + nums[i]); max = Math.max(max, curr); } return max; }
29. Merge Sorted Arrays
public int[] mergeSortedArrays(int[] a, int[] b) {
int[] result = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { result[k++] = a[i] < b[j] ? a[i++] : b[j++]; } while (i < a.length) result[k++] = a[i++]; while (j < b.length) result[k++] = b[j++]; return result; }
30. Intersection of Two Arrays
public int[] intersection(int[] nums1, int[] nums2) {