Skip to content

Bug Report for string-encode-and-decode #5373

@Yavdhesh

Description

@Yavdhesh

Bug Report for https://neetcode.io/problems/string-encode-and-decode

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Image

I have written below code-

In the decode method- Line 'if(str.charAt(i) == '$'){' does not compile.

        StringBuilder sb = new StringBuilder();

        for(int i=0; i<strs.size(); i++){
            String str = strs.get(i);
            sb.append(str.length()+"$");
            sb.append(str);
        }

        return sb.toString();

    }

    public List<String> decode(String str) {

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

        int cursor=0;
        int i = 0;
        while(i < str.length()){

            if(str.charAt(i) == '$'){
                int length = Integer.parseInt(str.substring(cursor, i));
                String originalToken = str.substring(i+1, i+1+length);
                list.add(originalToken);
                i = i + 1 + length;
                cursor = i;
            } else {
                i++;
            }

        }

        return list;

In

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions