Last active
October 4, 2018 02:46
-
-
Save backslash112/8c5b1ee68686cbbfddd391609c3a90a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Main { | |
public static void main(String[] args) { | |
System.out.println(modifyStr("").equals("")); | |
System.out.println(modifyStr("Automotive parts").equals("A6e p3s")); | |
System.out.println(modifyStr("Automotive par").equals("A6e p1r")); | |
System.out.println(modifyStr("Automotive pa").equals("A6e p0a")); | |
System.out.println(modifyStr("Automotive p").equals("A6e p0p")); | |
System.out.println(modifyStr("**Automotive p").equals("**A6e p0p")); | |
System.out.println(modifyStr("*Automotive* p").equals("*A6e* p0p")); | |
} | |
static String modifyStr(String input) { | |
// empty check | |
if (input == null || input.equals("")) return input; | |
String output = ""; | |
int wordStart = 0; | |
int delimit = 0; | |
while (wordStart < input.length()) { | |
// find the start index of word | |
while (!Character.isAlphabetic(input.charAt(wordStart))) { | |
output += input.charAt(wordStart); | |
wordStart++; | |
} | |
// find the delimit index of word | |
delimit = wordStart + 1; | |
while (delimit < input.length() && | |
Character.isAlphabetic(input.charAt(delimit))) { | |
delimit++; | |
} | |
// find the count of distinct letters | |
HashSet<Character> set = new HashSet<>(); | |
for (int i = wordStart+1; i < delimit-1; i++) { | |
set.add(input.charAt(i)); | |
} | |
output += String.valueOf(input.charAt(wordStart)) + set.size() + | |
String.valueOf(input.charAt(delimit-1)); | |
wordStart = delimit; | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PP 1.6: In the language of your choice, write a method that modifies a string using the following rules:
distinct letters between the first and last letter, and the last letter of the word. For example,
“Automotive parts" would be replaced by "A6e p3s".
characters.
relative location.