Question

In: Computer Science

Project: Given a string s and an integer array indices of the same length. The string...

Project:

Given a string s and an integer array indices of the same length.

The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string.

Return the shuffled string.

Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will need 3 methods:

1. Public static boolean validateInput(String s, int[] indices) --- inside ShuffledStringApp class, Check if the input string is not empty and the length of input array is the same length as the string, there are no duplicated numbers in the array, and the max number should not be greater than the length.

2. Public static String shuffle(String s, int[] indices) --- inside ShuffledStringApp class, process the string s.

3. Public static void main(string[] args) --- Prompt user to input a word and array. Call validateInput method for validation (If the input is not validate, repeat for user input) and shuffle method for shuffle the string. Print out the result.

Solutions

Expert Solution

Code:


import java.util.*;
public class ShuffledStringApp
{
public static boolean validateInput(String s,int[] indices){
//if Sting is empty or array length not equal to string length return false
if(s.length() == 0 && s.length()!=indices.length){
return false;
}
else{
for(int i=0;i<s.length();i++)
{
boolean isPresent=false;
//check if every index from 0 to lengh-1 is present or not
for(int j=0;j<indices.length;j++)
{
if(indices[j]==i)
{
isPresent=true;
break;
}
}
if(isPresent==false)
return false;
}
}
return true;
}
public static String shuffle(String s,int[] indices){
char res[] = new char[indices.length];
String result="";
for(int i=0;i<indices.length;i++)
res[indices[i]]=s.charAt(i);
  
for(int i=0;i<indices.length;i++)
result = result + res[i];
  
return result;
}
  
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
boolean validinput=false;
String s="";
int[] indices={0};
while(validinput==false)
{
System.out.println("\nEnter String:");
s=sc.next();
System.out.println("\nEnter indices values:");
indices=new int[s.length()];
for(int i=0;i<s.length();i++)
indices[i]=sc.nextInt();
validinput=validateInput(s,indices);
if(validinput==false)
System.out.println("\nInvalid input Enter again");
}
System.out.println("Shuffled String is "+ shuffle(s,indices));


}
}

Output:


Related Solutions

You are given two integer arrays a and b of the same length. Let's define the...
You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements: difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]| You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by...
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that...
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that denotes distance, determine if all the 1’s are at least k spaces away from each other. Details: The distance between each 1 in the array can be greater than or equal to k places. It doesn’t have to be exactly k Assume the array will only contain 0’s and 1’s – no need to do validation checking If k is greater than the size...
Given an array of positive numbers and a positive number S, find the length of the...
Given an array of positive numbers and a positive number S, find the length of the smallest contiguous subarray whose sum is greater than or equal to S. Return 0, if no such subarray exists. Example 1 Input: [2, 1, 5, 2, 3, 2], S=7 Output: 2 Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2]. Example 2 Input: [2, 1, 5, 2, 8], S=7 Output: 1 Explanation: The smallest subarray with a...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
rogram that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT