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 1 month, 4 weeks 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 1 month, 4 weeks ago by freddiec341.
        • This reply was modified 1 month, 4 weeks ago by Avatar photokees_b.
Viewing 0 reply threads