2 Easy Approaches 🔥🔥🔥🔥
\n# Brute Force Approach\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n\nclass Solution {\n    public String reverseStr(St
\n# Brute Force Approach\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n    public String reverseStr(String s, int k) {\n\n    int i=0;\n\n    String ans="";\n\n    while(i<s.length()){\n    if(i+(2*k)-1<s.length()||i+(2*k)-1>=s.length()&&s.length()-i>=k){\n    int index=i+(2*k)-1;\n    StringBuilder str=new StringBuilder();\n    String app=s.substring(i,i+k);\n    str.append(app);\n    str.reverse();\n    ans+=str.toString();\n    i=i+k;\n    while(i<s.length()&&i<=index){\n        ans+=s.charAt(i);\n        i++;\n    }\n    }\n    else{ \n    StringBuilder temp2=new StringBuilder();\n    temp2.append(s.substring(i,s.length()));\n    temp2.reverse();\n    ans+=temp2.toString();\n    break;\n    }\n    }\n    return ans;\n    }\n}\n```\n\n# Optimized Approach\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n    public String reverseStr(String s, int k) {\n\n    int i=0;\n\n    String ans="";\n\n    while(i<s.length()){\n        \n    if(i+(2*k)-1<s.length()||(i+(2*k)-1>=s.length()&&s.length()-i>=k)){\n    int index=i+(2*k)-1;\n    StringBuilder str=new StringBuilder();\n    String app=s.substring(i,i+k);\n    str.append(app);\n    str.reverse();\n    ans+=str.toString();\n    i=i+k;\n    if(index<s.length()){\n        ans+=s.substring(i,index+1);\n    }\n    else{\n        ans+=s.substring(i,s.length());\n    }\n\n    i=index+1;\n\n    }\n\n    else{ \n    StringBuilder temp2=new StringBuilder();\n    temp2.append(s.substring(i,s.length()));\n    temp2.reverse();\n    ans+=temp2.toString();\n    break;\n    }\n\n    }\n\n    return ans;\n\n    }\n}\n```\n
['Two Pointers', 'String', 'Java']