Question

  • Creator
    Topic
  • #4011757

    I can’t find an error in the Java code

    by HarryStoun ·

    I wrote the source code for a new application on IOS, when writing there were no errors, but when I ran the code, an error appeared in this place:
    Code:
    var t = readLine()!
    var s = readLine()!
    var len_s = s.count
    var t_lis = Set(t)
    var c_s = Counter(s)
    var c_t = Counter(t_lis[len_s])
    var c_res = [String: String]()
    var summ = 0
    for e in c_s{
    c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
    summ += c_res[e][1]}
    for i in (t.count-s.count) + 1 {
    if summ == len_s-1{
    print(i)
    break

    }
    if t[i] in c_res
    if c_res[t[i]][1] > 0{
    c_res[t[i]][1] -= 1
    summ -= 1
    }
    if i+len_s < t.count && t[i+len_s] in c_res
    if c_res[ t[i+len_s] ][1] < c_res[ t[i+len_s] ][0]{
    c_res[ t[i+len_s] ][1] += 1
    summ += 1

    }
    else
    print(-1)
    }
    Who can tell what is wrong?

    • This topic was modified 6 months ago by Avatar photokees_b.

You are posting a reply to: I can’t find an error in the Java code

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Answers

  • Author
    Replies
    • #4011815
      Avatar photo

      Re: java

      by kees_b ·

      In reply to I can’t find an error in the Java code

      This doesn’t look like a full Java method, so it’s quite understandable the Java interpreter gives an error. Then it’s up to you to fix it.

      May I remark that print(-1) doesn’t make much sense in an iOS app?

      • #4011825

        Reply To: I can’t find an error in the Java code

        by freddiec341 ·

        In reply to Re: java

        The error is likely due to the fact that you are attempting to access elements in an array that do not exist. In the code, you are accessing elements in the array t_lis with the index len_s. However, since len_s is the length of the string s, it will be larger than the length of t_lis, which will result in an out of bounds error. To fix this, change the line of code to access the last element in t_lis instead of using the index len_s.

        Note: irrelevant link removed by moderator.

        • This reply was modified 6 months ago by freddiec341.
        • This reply was modified 6 months ago by Avatar photokees_b.
    • #4079669

      Java Code Error Solution

      by jongore219 ·

      In reply to I can’t find an error in the Java code

      It seems like the error is due to a syntax issue in the following line:

      css
      Copy code
      c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
      The issue is that the square brackets [] are being used to create a list, but the type of c_res[e] is a dictionary. To fix this issue, you can change the square brackets to regular parentheses to create a tuple instead, like this:

      css
      Copy code
      c_res[e] = (c_s[e], min( c_s[e], c_t[e] ))
      In addition to this, there are a few other things that you may want to consider:

      1- The loop condition for i in (t.count-s.count) + 1 may not be what you intended. It will only loop once if t.count-s.count is equal to zero, and it will loop one time too many if t.count-s.count is greater than one. You may want to revise this condition to ensure that it loops over the correct range.

      2- The if statements that check if t[i] in c_res and t[i+len_s] in c_res are missing colons at the end. You should add colons to the end of these statements to make them syntactically correct.

Viewing 1 reply thread