-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbbreviationWords.java
More file actions
107 lines (96 loc) · 4.2 KB
/
Copy pathAbbreviationWords.java
File metadata and controls
107 lines (96 loc) · 4.2 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* AbbreviationWords.java
*
* Copyright 2017 Jorge <JPolanco@Emmthias>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
/********************************
* DESCRIPTION
********************************
* Find all the abbreviations of string:
* eg
* ABC
* SOME Valid abbreviations are :
* 1BC
* 2C
* 3
* A1C
* AB1
* A2
* NOT VALID
* 11C(two numbers cannot occur continuously)
*/
public class AbbreviationWords {
public static void main (String args[]) {
/********************************
* Questions
********************************
* the Min length of the String
* Should I need to validate for Empty or null?
* Should exist duplicated values?
*
* UPDATE
* 1)As 2 numbers cannot occur continously, the entry should not contains numbers.
* 2) someone else allows combinations between letter eg. A1C1, I NEED TO PUT MORE ATTENTION
* TO ASK WHICH SHOULD BE THE OUTPUT (I follow the test case, but I need to check it).
*/
/********************************
* Complexity
********************************
* O(n²)
* Since the String.length() needs to be loop per
* each letter (same size) the complexity it's square
* */
String test = "ABCD";
//Iterate over the length of word
String pre ="";
for(int i=0;i<test.length();i++){
calculateValidAbbreviation(test,pre,i);
pre += test.charAt(i);
}
System.out.println("Last Abbreviation " + test.length());
}
public static void calculateValidAbbreviation(String currentWord,String pre,int distance) {
String post = "";
System.out.println("*****************************************");
System.out.println("* distance ["+distance+"] *");
System.out.println("*****************************************");
for(int i=1;i<currentWord.length();i++){
//condition break
//for the last one cicle exist an overflow, because expect to print n-1 but get n-2
if((i+distance)>currentWord.length()) {
break;
}
post = currentWord.substring(i+distance,currentWord.length());
System.out.println(pre + i + post);
}
/***************************************************************************************
* distance * i * pre * post * result *
***************************************************************************************
* 1 * 1 * "" * BC * 1BC *
* 1 * 2 * "" * C * 2C *
***************************************************************************************
* 2 * 1 * A * C * A1C *
* 2 * 2 * A * C * A2 *
***************************************************************************************
* 3 * 1 * AB * "" * AB1 *
* 3 * 2 * AB * "" * AB2 * X
* */
}
}