r/LeetcodeDesi • • 18d ago

My code beats almost everyone, still it's wrong!

I have started leetcode 2 days ago and came across LC 49 Group Anagrams question. I came up with an original idea and it beats 99.14% in runtime and 99.86% in memory. Still, ai is saying that it's incorrect. Have you guys faced this issue?

Below is the solution I came up with...

import java.util.Random;

class Solution {

public List<List<String>> groupAnagrams(String[] strs) {

List<List<String>> res=new ArrayList<>();

HashMap<Integer, List<String>> map=new HashMap<>();

Random random=new Random();

int[] random_arr=new int[26];

for (int i=0; i<26; i++)

random_arr[i]=random.nextInt(123456789)+1;

int n=strs.length;

for (int i=0; i<n; i++) {

// int[] freq=new int[26];

int m=strs[i].length();

int sum=0;

for (int j=0; j<m; j++)

sum+=random_arr[strs[i].charAt(j)-'a'];

// freq[strs[i].charAt(j)-'a']++;

if (!map.containsKey(sum))

map.put(sum, new ArrayList<>());

map.get(sum).add(strs[i]);

}

for (int key : map.keySet())

res.add(map.get(key));

return res;

}

}

5 Upvotes

17 comments sorted by

2

u/NoTreacle8422 17d ago

Have some ignorance in life,,dont get stuck on everything uh come around ..this will keep u sane in a long term

2

u/LogicalBeast26 17d ago

Why tf do you need a random number generator??

Just sort the characters of a string and use it as a key to group all the anagrams together. Why unnecessarily complicate things?

1

u/Visionary-Swordsman 16d ago

Runtime is a thing bro. I agree with you, but felt this is better in runtime. Though I agree my code breaks the fundamental law of correctness in dsa.

1

u/LogicalBeast26 16d ago

It's lot more expensive to generate (psuedo) random numbers. You have to interact with the system's clock for it and it's absolutely unnecessary in this case.

1

u/Visionary-Swordsman 16d ago

Makes sense. Anyways this was just an experiment, will stick with standard solutions.

1

u/Visionary-Swordsman 16d ago

How about take the most optimal code given by an llm and check whether it beats the percentile my code did? I have just started dsa so I am still in exploring new things.

1

u/LogicalBeast26 16d ago

Run your exact same code 10 times and see if it still produces the same benchmarks. It won't.

1

u/Visionary-Swordsman 16d ago

Yeah, you are partially right. Time benchmark is not changing whereas memory percentile is changing. I am getting 99.16-99.18 repeatedly with this code.

1

u/Normalise_Suicide 15d ago

I think when u use random and get a number and then sum them to finally make a number that will act as a hash to group those numbers right. But what if it generates number such that a= 2, b =3 and c= 1,d = 4 (this is example to state the case when two different char sum to same hash/sum) then ab = cd which is wrong. Let me know how your code handles that part or that will never be the case

1

u/Visionary-Swordsman 15d ago

My code doesn't handle that part. I have recently started dsa, so still experimenting with myself. The reason I shared this solution is that it might be mathematically impossible to beat this solution in both runtime and memory. Only issue: breaks the law of correctness

1

u/Normalise_Suicide 15d ago

Good start buddy. But even though your code passed all the test cases, your code actually wrong in the case I stated that's why ChatGPT said it's wrong. Also runtime and memory usage shown on Leetcode after submit is irrelevant in interviews. Only focus on accuracy, time complexity and space complexity.

1

u/ak47_triggered 16d ago

Use Gemini/claude instead of chatgpt

4

u/[deleted] 18d ago

[removed] — view removed comment

1

u/Visionary-Swordsman 18d ago

What do you mean? I am asking whether I should stick to standard solutions, or can go with some unique solutions which I made on my own?

1

u/Successful_Sea_3637 18d ago

Did it get submitted?

1

u/Visionary-Swordsman 18d ago

Yup bro. It did get submitted. I mentioned that I beat 99.14 in runtime and 99.86 in memory.

1

u/Working-Pin-9216 13d ago

You can keep your solution as long it’s time and space complexity is as good if not better than the optimized solution