-
Notifications
You must be signed in to change notification settings - Fork 51
/
Question15.java
70 lines (56 loc) · 1.7 KB
/
Question15.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
class Dictionary
{ char chrs[]= new char[26];
Dictionary()
{
//generating array of characters
for(int i=0;i<26;i++) {
chrs[i]=(char)((int)'a'+i);
}
}
boolean check(String s,int[] distance)
{
int comp;
//Initialize flag true here which will be changed to false only when the the number of characters between the two letter is not specified in distance
boolean flag=true;
//Traversing through alphabets.
for(int i=0;i<26;i++) {
//Checking if the alphabet exists in the string
if(s.indexOf(chrs[i])!=-1) {
//getting the number of characters between two occurrences of the letters
comp=s.lastIndexOf(chrs[i])-s.indexOf(chrs[i])-1;
//checking if the number of characters between the two letter is does not match the value specified in distance
if(comp!=distance[i]) {
flag=false;
}
}
}
return flag;
}
}
public class Question15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Dictionary ab=new Dictionary();
int distance[]=new int[26];
//Accepting the String from the user
System.out.println("Enter the String:");
String s=sc.nextLine();
//Converting to string lowercase to make the code less case sensitive
s=s.toLowerCase();
//Accepting the distance values
System.out.println("Enter the distances:");
for(int i=0;i<26;i++) {
distance[i]=sc.nextInt();
}
//Checking if the string is well spaced or not
//the .check method in dictionary will determine if the string is well-spaced or not
if(ab.check(s,distance)) {
System.out.println("It is a well spaced string");
}
else {
System.out.println("It is not a well spaced string");
}
sc.close();
}
}