I do get the right output after more than 4 hours of trying all sorts but unfortunately it doesn't pass the test.
package com.codegym.task.task07.task0716;
import java.util.ArrayList;
/*
R or L
*/
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add("rose"); // 0
list.add("love"); // 1
list.add("lyre"); // 2
list.add("role");
list.add("nice");
list.add("four");
list = fix(list);
for (int i= 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
// write your code here
for(int i = 0; i < list.size();){//loop over the list
if(list.get(i).contains("r") && !list.get(i).contains("l"))
{// if string contains the letter r remove it
list.remove(i);
if(i != 0) i--;
}
if(list.get(i).contains("l") && !list.get(i).contains("r"))
{// if string contains the letter l and not an r duplicate it
list.add(i, list.get(i));//list.get(i) gives the value of that specific value on index i
i++;
}
/*if((list.get(i).contains("r")) && (list.get(i).contains("l"))) //
{// if string contain r & l don't do anything and move on to the next item
i++;
}
else
{//if string does not contain r || l don't do anything
i++;
}*/
}
return list;
}
}