Problem:
Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.
sameStarChar("xy*yzz") → true
sameStarChar("xy*zzz") → false
sameStarChar("*xa*az") → true
Solution:
public boolean sameStarChar(String str) { int len = str.length(); boolean found = true; for (int i = 0; i < len; i++) { String tmpString = str.substring(i,i+1); if (tmpString.equals("*") && i > 0 && i < len-1) { if (str.charAt(i-1) == str.charAt(i+1)) found = true; else found = false; } } return found; }
public boolean sameStarChar(String str) {
ReplyDeletefor (int i=1; i<str.length()-1; i++) {
if (str.charAt(i) == '*') {
if (str.charAt(i-1) != str.charAt(i+1)) return false;
}
}
return true;
public boolean sameStarChar(String str) {
ReplyDeletefor(int i=0; i=1 && str.charAt(i)=='*' && str.charAt(i-1)!=str.charAt(i+1)) return false;
return true;
}
public boolean sameStarChar(String str) {
ReplyDeleteint star = str.lastIndexOf("*", str.length()-2);
if(str.length()<3 || !str.contains("*")) return true;
return str.charAt(star-1)==str.charAt(star+1);
}
public static boolean sameStarChar(String str) {
ReplyDeleteString input=str;
for(int i=0;i<input.length();i++) {
if(input.charAt(i)=='*'&&i!=0&&i!=input.length()-1) {
if(input.charAt(i-1)==input.charAt(i+1)){
return true;
}
}
}
return false;
}
boolean res=true;int option=0;
ReplyDeleteif(!str.contains("*"))
{
return true;
}
if(str.length()==0)
{
return true;
}
if(str.length()==1)
{
if(str.charAt(0)=='*')
{
return true;
}
}
if(str.length()==2)
{
if(str.charAt(0)=='*'&&str.charAt(1)=='*')
{
return true;
}
}
for(int i=1;i<str.length()-1;i++)
{
if(str.charAt(i)=='*')
{
if(str.charAt(i-1)==str.charAt(i+1))
{
// System.out.println(true);
option=1;
}
if(str.charAt(i-1)!=str.charAt(i+1))
{
// System.out.println(true);
option=2;
}
}
}
if(option==1)
{
return true;
}
return false;
public boolean sameStarChar(String str)
ReplyDelete{
boolean result = true;
int length = str.length();
int pos = str.indexOf("*");
do {
if(pos>0 && pos0);
return result;
}
for(int i=0;i0&&i<str.length()-1))
ReplyDelete{
if(str.charAt(i-1)!=str.charAt(i+1))
return false;
}
}
return true;
public String oneTwo(String str) {
ReplyDeleteif( str.length() <= 2 ){
return "";
}
return "" + str.charAt(1) + str.charAt(2) + str.charAt(0)
+ oneTwo(str.substring(3));
}
public String oneTwo(String str) {
ReplyDeletereturn ( str.length() <= 2 ) ? ""
:"" + str.charAt(1) + str.charAt(2) + str.charAt(0)
+ oneTwo(str.substring(3));
}
public boolean sameStarChar(String str) {
ReplyDeleteboolean res = true;
for (int i=1;i<str.length()-1;i++) {
if (str.charAt(i)=='*') {
if (str.charAt(i-1) == str.charAt(i+1))
res = true;
else
res = false;
}
}
return res;
}
public boolean sameStarChar(String str) {
ReplyDeletefor (int i = 1; i<str.length()-1; i++) {
if (str.charAt(indexOf("*")+1)!=str.charAt(indexOf("*")-1))) {
return false;
}
}
return true;
}
Why does this give a missing '}' or illegal start of expression error?
one more brace at the end of the line 3. ) <--- such line
Deletepublic boolean sameStarChar(String str) {
ReplyDeleteif (!str.contains("*")) return true;
else if (str.length()<3) {
for (int i=0; i<str.length() ; i++){
if (str.charAt(i)!='*') return false;
}
}
for (int i=1; i<str.length()-1 ; i++){
if (str.charAt(i)=='*' && str.charAt(i-1) != str.charAt(i+1)) return false;
}
return true;
}
the shortest way:
ReplyDeletepublic boolean sameStarChar(String str) {
for(int i = 1; i < str.length() - 1; i++){
if(str.charAt(i) == '*' && str.charAt(i - 1) != str.charAt(i + 1)){
return false;
}
}
return true;
}