String Scramble Coderbyte Solution

0

Have the function StringScramble(str1,str2) take both parameters being passed and return the string true if a portion of str1 characters can be rearranged to match str2, otherwise return the string false. 


For example: if str1 is "rkqodlw" and str2 is "world" the output should return true. Punctuation and symbols will not be entered with the parameters.

String scramble coderbyte java


import java.util.Arrays;
import java.util.Scanner;


public class StringScramble {
boolean StringScramble(String str1, String str2) {
char[] str1Chars = str1.toCharArray();
char[] str2Chars = str2.toCharArray();

boolean[] used = new boolean[str1Chars.length];
Arrays.fill(used, false);

Arrays.sort(str1Chars);
Arrays.sort(str2Chars);

for (char c : str2Chars) {
boolean found = false;
for (int i = 0; i < str1Chars.length; i++) {
if (str1Chars[i] == c && !used[i]) {
used[i] = true;
found = true;
break;
}
}

if (!found) return false;
}

return true;

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
StringScramble c = new StringScramble();
System.out.print(c.StringScramble("coodrebtqqkye", "coderbyte"));
}
}

String scramble coderbyte python


def StringScramble(str1, str2):
outer_string = str1
inner_string = str2
for char in inner_string:
if char not in outer_string:
return False
updated_outer = outer_string.replace(char, "", 1)
outer_string = updated_outer
return True

String scramble javascript


function StringScramble(str1, str2) {

let filteredStr1 = str1.replace(/[^0-9a-zA-Z]/gi, '');

let filteredStr2 = str2.replace(/[^0-9a-zA-Z]/gi, '');

for (let i = 0; i < filteredStr2.length; i++) {

if (filteredStr1.indexOf(filteredStr2[i]) === -1) {

return false;
}

filteredStr1 = filteredStr1.replace(filteredStr2[i], '');
}

return true;
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !