# [C++] [Java] Clean Code

• C++

``````class Solution {
public:
bool judgeCircle(string moves) {
int v = 0;
int h = 0;
for (char ch : moves) {
switch (ch) {
case 'U' : v++; break;
case 'D' : v--; break;
case 'R' : h++; break;
case 'L' : h--; break;
}
}
return v == 0 && h == 0;
}
};
``````

Java

``````public class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for (char ch : moves.toCharArray()) {
if (ch == 'U') y++;
else if (ch == 'D') y--;
else if (ch == 'R') x++;
else if (ch == 'L') x--;
}
return x == 0 && y == 0;
}
}
``````

• Same idea

``````class Solution {
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
if(c == 'U') {
x++;
}else if(c == 'D') {
x--;
}else if(c == 'L') {
y--;
}else {
y++;
}
}
return x == 0 && y == 0;
}
}
``````

• Why didn't you guys use switch statement in Java code? I think having those multiple if-else statements is a bad practice and the switch statement fits the best in this problem.

``````    public boolean judgeCircle(String moves) {
int v = 0, h = 0;
for (char move : moves.toCharArray()) {
switch (move) {
case 'U': v++; break;
case 'D': v--; break;
case 'R': h++; break;
case 'L': h--; break;
}
}
return v == 0 && h == 0;
}

``````

• Thanks for sharing the solutions. It seems like switch is more readable. Performance wise, it beats 90% but "else if" statements are > 60%, I was wondering the reason why switch is faster. Could anyone explain that? Thanks.

• @alexander Nice. Looks like it will be better to add "else if" or "break" in the java solution to avoid unnecessary checks.

• @Alexander Hello, for the input case "RLUURDDDLU" my code does not work correctly according to solution, but I have tested many times the output should be also true according to my code but it gives false. Is there any problem could you please help me since this solution is pretty straightforward

Thank you

``````bool judgeCircle(string moves) {

int u, l, r, d = 0 ;
for(int i = 0 ; i < moves.size() ; i++)
{

if(moves[i] == 'U')
u++;
else if(moves[i] =='D')
d++;
else if(moves[i] == 'L')
l++;
else if(moves[i] == 'R')
r++;
}

if (u == d &&  l==r)
return true;
return false;
}``````

• @Yueyi-Wang The history of Java switch statement has a problem (prior to jdk 1.7) like only valid for non-long type. Even after jdk 1.7, If you pass a String type variable, you cannot pass the variable like Object str = "foo" to switch statement. It has other minor issues you need be careful when you want to use it. I guess that's the main reason for people like me refuse to use Java switch.

• I think you did not consider the case when the input contains invalid parameters, such as "UDp", your code will return true, but it should return false.

• @kenan3 I'm not familiar with Java switch, but generally speaking, switch is faster than if-else(even though it's small tho) and make it logically clear - All cases have the same access time; It comes with the same order.

Not I'm not sure if most modern compilers optimize deep if/else..if else point as a switch/jump table.

• @Yueyi-Wang maybe they don't like break.

• @new2500 Thanks for pointing this out. You are right. When the size of if-else statements are big, we can get benefits from changing from if-else to switch-case.

I should've said "I prefer to use if-else when the if-else statements are smaller than 5 or something".

A minor problem of your post "All cases have the same access time" is not always true. Since CPU has prediction and pre-fetch instruction mechanism. Branches are always the performance bottleneck, so it cannot guarantee the same access time (especially when those instructions are supposed to be executed multiple times).

For your last statement, I think most `static compilers for non-VM (it's not a term) languages` can do this optimization very easily. But for java, it would leave optimization until JIT at runtime.

• you guys all seem to forget that if you go left, left, left, left (LLLL), you're back to the original spot. But the code would not work for this.

• @huyuci I think in here left means not making a turn and turn left, but just literally left.

• @alexander Gotcha. Silly me :-) I didn't read carefully

• i had the exact same solution.

• What would the space efficiency of this solution be? Here is what I believe the time and space efficiency are. Please discuss/correct me if I am wrong.

Time: O(n)

• We are making a single pass through the characters of this string so the time complexity would be O(n) where n is the number of characters in the string.

Space: O(n)?

• My reasoning for thinking that space efficiency would be O(n) would be that we need to store the characters as an array in memory. This array would be n characters long so it would be O(n).

I'm pretty sure that time complexity is right but is the space complexity correct?

• C++

``````class Solution {
public:
bool judgeCircle(string moves) {
int v = 0;
int h = 0;
for (char ch : moves) {
switch (ch) {
case 'U' : v++; break;
case 'D' : v--; break;
case 'R' : h++; break;
case 'L' : h--; break;
}
}
return v == 0 && h == 0;
}
};
``````

Java

``````public class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for (char ch : moves.toCharArray()) {
if (ch == 'U') y++;
else if (ch == 'D') y--;
else if (ch == 'R') x++;
else if (ch == 'L') x--;
}
return x == 0 && y == 0;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.