Solution in Java for couint and Say


#1

.public class Solution {
public String countAndSay(int A) {
String countedString = “1”;
for(int i = 1; i < A; i++){
StringBuilder stringBuilder = new StringBuilder();
int countActualChar = 1;
char oldChar = countedString.charAt(0);
for(int j = 1; j < countedString.length(); j++){
char nextChar = countedString.charAt(j);
if(nextChar==oldChar){
countActualChar++;
}else{
stringBuilder.append(countActualChar);
stringBuilder.append(oldChar);
countActualChar = 1;
}
oldChar=nextChar;
}
stringBuilder.append(countActualChar);
stringBuilder.append(oldChar);
countedString = stringBuilder.toString();
}
return countedString;
}
}