Java solution not working? SOS


  • -1
    S

    Anyone have any idea why this fails?

    Thank you very much

    public class Solution {
        public boolean isSubsequence(String s, String t) {
            int index = 0;
            int subIndex = 0;
            
            if (s.length() > t.length()) {
                return false;
            }
            
            while(subIndex < s.length()) {
                char c = s.charAt(subIndex);
                index = t.substring(index, t.length()).indexOf(c);
                if ( index == -1) { return false;}
                subIndex++;
            }
            
            return true;
        }
    }
    

  • 0

    You know a bit about why it fails, namely how you failed (Wrong Answer? Time Limit Exceeded? Something else?) and at what input and what your output and the expected output are. Why are you not sharing that information?


  • 0
    S

    @ManuelP I am getting a wrong answer for a certain input... the input is extremely long but if you're willing to check it out try to copy and paste the code to the problem!

    https://leetcode.com/problems/is-subsequence/

    Thanks!!


  • 0

    That input is only long because of the long substrings of irrelevant 'y' characters. If you replace each of them with just a single 'y', the test case is just "leeeeetcode", "ylyeyeyeytycyoydyey", which you fail as well. And it's small enough to show here and should be small enough for you to conveniently debug, I think.

    If you remove every 'y', then it becomes "leeeeetcode", "leeetcode", so at least it's clear that the answer should be false and your output of true (for the real input) is indeed wrong.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.