Showing posts with label leetcode. Show all posts
Showing posts with label leetcode. Show all posts

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 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;
    }
}

Leetcode 2894. Divisible and Non-divisible Sums Difference

 

Problem Statement:

 leetcode problem 2894. Divisible and Non-divisible Sums Difference


You are given positive integers n and m.

Define two integers as follows:num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.
num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.

Return the integer num1 - num2.


Initial Code

class Solution {
    public int differenceOfSums(int n, int m) {
        int suma = 0;sumb=0; // ERROR: sumb not declared properly
        for (int i=1; i==n; i++){ // ERROR: i==n (this never iterates)
            if(i%m==0){
                sumb+=i;
            }else{
                suma+=i;
            }
        }
        return suma-sumb;
    }
}


Issues in the code:

  • Variable declaration: sumb is undeclared.
  • Loop condition: for (int i = 1; i == n; i++) executes only when i equals n, so it never loops. It must be i <= n.
  • Minor style: combining declarations on one line makes it harder to read

Corrected code

class Solution {
    public int differenceOfSums(int n, int m) {
        int suma = 0;
        int sumb = 0;
        for (int i = 1; i <= n; i++) {
            if (i % m == 0) sumb += i;
            else suma += i;
        }
        return suma - sumb;
    }
}

Leetcode 3110 - Score of a string problem analysis

 Hi Everyone, This was the first attempt answer for the Leetcode problem on score of a string. 

https://leetcode.com/problems/score-of-a-string

class Solution {
    public int scoreOfString(String s) {
        sum = 0;
        char[] ssize = s.toCharArray();
        for (int i=0;i<ssize.length-1; i++){
            sum = sum + abs(ssize(i).asciicode-ssize(i+1).asciicode)
        }
            return sum
        }

Issues with the code - 

Issue Description
sum not declared   - sum is used without type declaration.
Incorrect syntax: abs() - Java requires Math.abs() for absolute value.
Incorrect character access: ssize(i) - Use ssize[i], not parentheses.
Non-existent property: .asciicode Characters in Java already act as ASCII values when cast to int.
Missing semicolons and braces Several semicolons and } missing.
Overall formatting and structure Needs improvement for clarity and compilation.


Learning from the code: 

1. Declare the variables. 
2. For each Math method, use Math.<method>
3. Array access using [].
4. asciiCode. each character has a ascii value, which can be used while converting to int.
5. colons are missing


Corrected code for this 


class Solution {
    public int scoreOfString(String s) {
        int sum = 0;
        char[] ssize = s.toCharArray();
        for (int i = 0; i < ssize.length - 1; i++) {
            sum += Math.abs(ssize[i] - ssize[i + 1]);
        }
        return sum;
    }
}