SimpleTestsJul012026

Run Settings
LanguageJava
Language Version
Run Command
/*Given an array a, your task is to output an array b of the same length by applying the following transformation: – For each i from 0 to a.length - 1 inclusive, b[i] = a[i - 1] + a[i] + a[i + 1] – If an element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, use 0 in its place – For instance, b[0] = 0 + a[0] + a[1] Example For a = [4, 0, 1, -2, 3]: – b[0] = 0 + a[0] + a[1] = 0 + 4 + 0 = 4 – b[1] = a[0] + a[1] + a[2] = 4 + 0 + 1 = 5 – b[2] = a[1] + a[2] + a[3] = 0 + 1 + (-2) = -1 – b[3] = a[2] + a[3] + a[4] = 1 + (-2) + 3 = 2 – b[4] = a[3] + a[4] + 0 = (-2) + 3 + 0 = 1 So, the output should be solution(a) = [4, 5, -1, 2, 1]. */ class Main { public static int matchCount(String pattern, String string){ int start=0, end= pattern.length(); int matchCount=0; while (end < string.length()){ int index= 0; boolean matchFound=true; while(start+index < end && matchFound){ if (pattern.charAt(index) == '0' && isVowel(string.charAt(start+index))){ matchFound=true; } else if (pattern.charAt(index) == '1' && isVowel(string.charAt(start+index))){ matchFound=false; } else if (pattern.charAt(index) == '0' && !isVowel(string.charAt(start+index))){ matchFound = false; } else if (pattern.charAt(index) == '1' && !isVowel(string.charAt(start+index))){ matchFound=true; } index++; } if (matchFound){ matchCount++; } start++; end++; } return matchCount; } public static boolean isVowel(char character){ return (Character.isLetter(character) && (character == 'A' || character == 'a' || character == 'E' || character == 'e' || character == 'I' || character == 'i' || character == 'O' || character == 'o' || character == 'U' || character == 'u' || character == 'Y' || character == 'y' )); } public static void main(String[] args) { System.out.println(""+matchCount("100", "amazing")); System.out.println(""+matchCount("101", "amazing")); System.out.println(""+matchCount("010", "amazing")); System.out.println(""+matchCount("11", "amazing")); System.out.println(""+matchCount("00", "amazing")); } }
Editor Settings
Theme
Key bindings
Full width
Lines