Codeforces Round 894 Div 3 Solution:
Problem Statement Link --> Problem - A - Codeforces
Solution in JAVA:
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int testCases = scanner.nextInt();
for (int testCase = 0; testCase < testCases; testCase++) {
int numRows = scanner.nextInt();
int numCols = scanner.nextInt();
List<String> carpetRows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
carpetRows.add(scanner.next());
}
int requiredColumns = 4;
String target = "vika";
int targetIndex = 0;
int selectedColumns = 0;
for (int col = 0; col < numCols; col++) {
boolean foundColumn = false;
for (int row = 0; row < numRows; row++) {
String rowContent = carpetRows.get(row);
char currentChar = rowContent.charAt(col);
if (currentChar == target.charAt(targetIndex)) {
targetIndex++;
foundColumn = true;
break;
}
}
if (foundColumn) {
selectedColumns++;
if (selectedColumns == requiredColumns) {
break;
}
}
}
if (selectedColumns == requiredColumns) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
scanner.close();
}
}
No comments:
Post a Comment