Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NAME OF THE STUDENT : SRIHARSHITHA DEEPALA

REGISTRATION NUMBER : 19BCD7246

SLOT : L27+L28+L31+L32

ASSIGNMENT NUMBER : 04

1. You are given a string s and an array of strings words of the same length.
Return all starting indices of substring(s) in s that is a concatenation of each
word in words exactly once, in any order, and without any intervening
characters.You can return the answer in any order.

Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar"
respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input: s = "wordgoodgoodgoodbestword", words =
["word","good","best","word"]
Output: []
Example 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]
CODE
import java.util.*;

class Question1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String S = scan.next();
int n = scan.nextInt();
ArrayList<String> words =
new ArrayList<>();
for(int i=0;i<n;i++){
String w = scan.next();
words.add(w);
}
ArrayList<Integer> indices = Substring(S, words);
System.out.println(indices);
scan.close();
}
public static ArrayList<Integer>
Substring(String A, final List<String> B)
{

int wordsize = B.get(0).length();


int word_count = B.size();
int listsize = wordsize * word_count;
ArrayList<Integer> res = new ArrayList<Integer>();
int n = A.length();
if (listsize > n)
{
return res;
}
HashMap <String, Integer> hashMap =
new HashMap<String, Integer>();
for (String word : B)
{
hashMap.put(word, hashMap.getOrDefault(word, 0) + 1);
}
for (int i = 0; i <= n - listsize; i++)
{
HashMap<String, Integer> tempMap =
(HashMap<String, Integer>) hashMap.clone();
int j = i, count = word_count;
while (j < i + listsize)
{
String word = A.substring(j, j + wordsize);
if (!hashMap.containsKey(word) || tempMap.get(word) == 0)
{
break;
}
else
{
tempMap.put(word, tempMap.get(word) - 1);
count--;
}
j += wordsize;
}
if (count == 0)
{
res.add(i);
}

}
return res;
}
}
OUTPUT

Test Case-1

Test Case-2

Test Case-3
2. Word Problem
Get a matrix from the user. Pick a minimum of one character from one row and
create the word. If the guessed word abides to the rule return 1 else 0.
Example :
Given board =
[
["ABCE"],
["ESFC"],
["XDEE"]
]

Sample input output:

AXE Output : 1
DOG Output : 0
BED Output : 1
CAT Output : 0
BASE Output: 1

CODE

import java.util.*;
class Main
{
static HashSet<String> set;
static void Func(String st,int i,String temp){
if(i>=st.length()){
set.add(temp);
return;
}

Func(st,i+1,temp+st.charAt(i));
Func(st,i+1,temp);
}
public static String sortString(String Str_input)
{

char tempArray[] = Str_input.toCharArray();


Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
ArrayList<String> list = new ArrayList<>();
while(n-->0){
String str = scan.next();

list.add(str);
}

String st="";
for(int i=0;i<list.size();i++){
st+=list.get(i);
}
st=sortString(st);

set = new HashSet<>();


Func(st,0,"");

int output =scan.nextInt();


while(output-->0){
String str = scan.next();
str=sortString(str);
if(set.contains(str))
System.out.println("Output : 1");

else
System.out.println("Output : 0");
}

}
}
OUTPUT

You might also like