Friday, July 11, 2025

Leetcode 2942. Find Words Containing Character

  

Problem Statement:

2942. Find Words Containing Character


You are given a 0-indexed array of strings words and a character x.

Return an array of indices representing the words that contain the character x.

Note that the returned array may be in any order.


Initial Code: 

class Solution {
    public List<Integer> findWordsContaining(String[] words, char x) {
        List<Integer> findWordsContaining = new ArrayList();
        for (int i = 0; i < words.length; i++) {
            if (words[i].contains(x)) {
                findWordsContaining.add(i);
            }
        }
        return findWordsContaining;
    }
}


Issue in the code - 


new ArrayList() - Should declare the type parameter: new ArrayList<>() (or new ArrayList<Integer>())
words[i].contains(x) -  String.contains() expects a CharSequence, but  passed char. Need to convert the char to a String:


Updated Code:


class Solution {
    public List<Integer> findWordsContaining(String[] words, char x) {
        List<Integer> findWordsContaining= new ArrayList<>();
        for (int i=0;i<words.length; i++){
            if(words[i].indexOf(x)!=-1){
                findWordsContaining.add(i);
            }
        }
        return findWordsContaining;
    }
}

Leetcode 2769. Find the Maximum Achievable Number

  

Problem Statement:

2769. Find the Maximum Achievable Number


Given two integers, num and t. A number x is achievable if it can become equal to num after applying the following operation at most t times:Increase or decrease x by 1, and simultaneously increase or decrease num by 1.

Return the maximum possible value of x.

Initial Code: 

class Solution {
    public List<Integer> findWordsContaining(String[] words, char x) {
        List<Integer> findWordsContaining = new ArrayList();
        for (int i = 0; i < words.length; i++) {
            if (words[i].contains(x)) {
                findWordsContaining.add(i);
            }
        }
        return findWordsContaining;
    }
}


Issue in the code - 


new ArrayList() - Should declare the type parameter: new ArrayList<>() (or new ArrayList<Integer>())
words[i].contains(x) -  String.contains() expects a CharSequence, but  passed char. Need to convert the char to a String:



Leetcode 1920 – Build Array from Permutation

  

Problem Statement:

1920. Build Array from Permutation


Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.

A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).

Initial Code

class Solution {
    public int[] buildArray(int[] nums) {
        int[nums.length] numsnew;
        for (int i=0;i<nums.length;i++){
            numsnew[i]=nums[nums[i]];
        }
        return numsnew;
    }
}


Issues in the code


LineIssue
int[nums.length] numsnew; - Invalid Java syntax for array declaration.
 Should be: int[] numsnew = new int[nums.length];



Corrected code

class Solution {
    public int[] buildArray(int[] nums) {
        int[] numsnew = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            numsnew[i] = nums[nums[i]];
        }
        return numsnew;
    }
}