Coding Challenge Day 23: Problem 2: Scramble or Remove

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 23: Problem 2! Be sure to post your answers, queries etc in the comments!

NOTE: This problem is a continuation of Coding Challenge Day 23: Problem 1. (LINK)

Problem: Write the method scrambleOrRemove, which replaces each word in the parameter wordList with its scrambled version and removes any words that are unchanged after scrambling. The relative ordering of
the entries in wordList remains the same as before the call to scrambleOrRemove.

Before the call to scrambleOrRemove:

“TAN” “ABRACADABRA”“WHOA”“APPLE”“EGGS”

After the call to scrambleOrRemove:

“TNA”“BARCADABARA”“PAPLE”

Solution:

import java.util.List;
import java.util.ArrayList;

public class scrambleproblem {

	public static String Scrambleword (String str){
		
		String scrambled="";
		
		for(int i=0; i<str.length(); i++){
			char c= str.charAt(i);
			
			if(i < (str.length()-1) && c=='A'){
				scrambled+= (char)str.charAt(i+1) + "A";
				i++;
			}
			
			else{
				scrambled+=c;
			}
		}
	
		
		return scrambled;
	}
	
public static void scrambleorRemove (List<String> wordlist){
		int index=0;
		
		while(index<wordlist.size()){
			String word1= wordlist.get(index);
			String word2= Scrambleword(word1);
			
			if(word1.equals(word2)){
				wordlist.remove(index);
			}
			
			else{
				wordlist.set(index, word2);
				index++;
			}
		}
		
		for(String j: wordlist){
			System.out.print(j + " ");
		}
	}
	
	public static void main (String [] args){
		List<String> wordlist= new ArrayList<String>();
		wordlist.add("TAN");
		wordlist.add("ABRACADABRA");
		wordlist.add("WHOA");
		wordlist.add("APPLE");
		wordlist.add("EGGS");
		
		scrambleorRemove(wordlist);
		
	}

}

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d
search previous next tag category expand menu location phone mail time cart zoom edit close