Reverse Words in a String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
    public String reverseWords(String s) {
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Wrong Answer

Input: " 1" Output: "1 " Expected: "1"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
    public String reverseWords(String s) {
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            // if(i > 0){
                sb.append(" ");    
            // }         
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Wrong Answer

Input: "" Output: " " Expected: ""


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Solution {
    public String reverseWords(String s) {
        if(s.equals("") || s.length() == 0){
            return null;
        }
        if(s.length() == 1){
            return s;
        }
        boolean head;
        boolean tail;
        if(s.charAt(0) == " "){
            head = true;
        }
        if(s.charAt(s.length() - 1) == " "){
            tail = true;
        }
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }
            
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Compile Error

Line 11: error: incomparable types: char and String


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
    public String reverseWords(String s) {
        if(s.equals("") || s.length() == 0){
            return null;
        }
        if(s.length() == 1){
            return s;
        }
        boolean head;
        boolean tail;
        if(s.charAt(0).isWhiteSpace()){
            head = true;
        }
        if(s.charAt(s.length() - 1).isWhiteSpace()){
            tail = true;
        }
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }    
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Compile Error

Line 11: error: char cannot be dereferenced


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
    public String reverseWords(String s) {
        if(s.equals("") || s.length() == 0){
            return null;
        }
        if(s.length() == 1){
            return s;
        }
        boolean head;
        boolean tail;
        if(s.charAt(0) == ' '){
            head = true;
        }
        if(s.charAt(s.length() - 1) == ' '){
            tail = true;
        }
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }       
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Compile Error

Line 19: error: variable tail might not have been initialized


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
    public String reverseWords(String s) {
        if(s.equals("") || s.length() == 0){
            return null;
        }
        if(s.length() == 1){
            return s;
        }
        boolean head = false;
        boolean tail = false;
        if(s.charAt(0) == ' '){
            head = true;
        }
        if(s.charAt(s.length() - 1) == ' '){
            tail = true;
        }
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }    
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Wrong Answer

Input: "" Output: null

Expected: ""

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
    public String reverseWords(String s) {
        if(s.equals("") || s.length() == 0 || s.length() == 1){
            return s;
        }
        boolean head = false;
        boolean tail = false;
        if(s.charAt(0) == ' '){
            head = true;
        }
        if(s.charAt(s.length() - 1) == ' '){
            tail = true;
        }
        String[] helper = s.split(" ");
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }    
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }}

Submission Result: Wrong Answer

Input: " " Output: " " Expected: ""


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Solution {
    public String reverseWords(String s) {
        String[] helper = s.split(" ");
        if(helper.size() == 0){
            return "";
        }
        // if(s == " "){
        //     return "";
        // }
        if(s.equals("") || s.length() == 0 || s.length() == 1){
            return s;
        }
        boolean head = false;
        boolean tail = false;
        if(s.charAt(0) == ' '){
            head = true;
        }
        if(s.charAt(s.length() - 1) == ' '){
            tail = true;
        }
        StringBuffer sb = new StringBuffer();
        if(tail){
            sb.append(" ");
        }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }   
        }
        if(head){
            sb.append(" ");
        }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Compile Error

Line 4: error: cannot find symbol: method size()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        // if(s == " "){
        //     return "";
        // }
        // 可能有多个空格
        String[] helper = s.split(" ");
        if(helper.length == 0){
            return "";
        }
        if(s.equals("") || s.length() == 0 || s.length() == 1){
            return s;
        }
        // boolean head = false;
        // boolean tail = false;
        // if(s.charAt(0) == ' '){
        //     head = true;
        // }
        // if(s.charAt(s.length() - 1) == ' '){
        //     tail = true;
        // }
        StringBuffer sb = new StringBuffer();
        if(helper.length == 1){
            sb.append(helper[0]);
            return sb.toString();
        }
        // if(tail){
        //     sb.append(" ");
        // }
        for(int i = helper.length - 1; i >= 0; i--){
            sb.append(helper[i]);
            if(i > 0){
                sb.append(" ");    
            }   
        }
        // if(head){
        //     sb.append(" ");
        // }
        String reverse = sb.toString();
        return reverse;
    }
}

Submission Result: Wrong Answer

Input: " 1" Output: "1 "

Expected: "1"

第二天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(s.charAt(i).isWhiteSpace()){
                i++;
            }else{
                break;
            }
        }
        int tail = l;
        while(tail >= 0){
            if(s.charAt(l).isWhiteSpace()){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.subString(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(' ');
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Compile Error

Line 10: error: char cannot be dereferenced

The type char is a primitive -- not an object -- so it cannot be dereferenced

Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.

use Character class:

if(Character.isLetter(c)) {}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhiteSpace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l;
        while(tail >= 0){
            if(Character.isWhiteSpace(s.charAt(i))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.subString(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(' ');
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Compile Error

Line 10: error: cannot find symbol: method isWhiteSpace(char)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(i))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.subString(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(' ');
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Compile Error

Line 25: error: cannot find symbol: method subString(int,int)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(i))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.substring(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(' ');
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Compile Error

Line 31: error: no suitable method found for split(char)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(i))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.substring(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Runtime Error

Runtime Error Message: Line 18: java.lang.StringIndexOutOfBoundsException: String index out of range: 1

Last executed input: " "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l - 1;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(i))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.substring(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Runtime Error

Runtime Error Message: Line 18: java.lang.StringIndexOutOfBoundsException: String index out of range: 1

Last executed input: " "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l - 1;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(tail))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        ss = s.substring(i, tail + 1);
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Runtime Error

Runtime Error Message: Line 25: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Last executed input: " "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l - 1;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(tail))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        if(tail > i){
            ss = s.substring(i, tail + 1);
        }
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Wrong Answer

Input: " " Output: " "

Expected: ""

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Solution {
    public String reverseWords(String s) {
        
        int l = s.length();
        if(l == 0){
            return s;
        }
        int i = 0;
        while(i < l){
            if(Character.isWhitespace(s.charAt(i))){
                i++;
            }else{
                break;
            }
        }
        int tail = l - 1;
        while(tail >= 0){
            if(Character.isWhitespace(s.charAt(tail))){
                tail--;
            }else{
                break;
            }
        }
        String ss = new String();
        if(tail >= i){
            ss = s.substring(i, tail + 1);
        }else{
            return "";
        }
        int ll = ss.length();
        if(ll == 0 || ll == 1){
            return s;
        }
        
        String[] helper = ss.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int j = helper.length - 1; j >= 0; j-- ){
            sb.append(helper[j]);
            if(j > 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
    }
}

Submission Result: Wrong Answer

Input: " 1" Output: " 1"

Expected: "1"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
    public String reverseWords(String s) {
        if(s.length() == 0 || s == null){
            return s;
        }
        
        if(s.length() == 1 && s != " "){
            return s;
        }
        
        s = s.trim();
        
        String[] helper = s.split("\\s+");
        
        StringBuffer sb = new StringBuffer();
        
        for(int i = helper.length - 1; i >= 0; i-- ){
            sb.append(helper[i]);
            if(i != 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
        
    }
}

Submission Result: Wrong Answer

Input: " " Output: " "

Expected: ""

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution {
    public String reverseWords(String s) {
        if(s.length() == 0 || s == null){
            return s;
        }
        
        if(s.length() == 1 && !s.equals(" ")){
            return s;
        }
//忘记用 trim()来去首尾的空格        
        s = s.trim();

        //这里注意所有空格的表达形式, 包括whitespace, tab, enter等 详见http://stackoverflow.com/a/225360/2345313
        String[] helper = s.split("\\s+");
        
        StringBuffer sb = new StringBuffer();
        
        for(int i = helper.length - 1; i >= 0; i-- ){
            sb.append(helper[i]);
            if(i != 0){
                sb.append(" ");
            }
        }
        
        return sb.toString();
        
    }
}

Submission Result: Accepted