Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8514261

Browse files
committed
Implemented the program to check if string is acronym of words or not.
1 parent 5005aab commit 8514261

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

.idea/workspace.xml

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Link: https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/
3+
4+
Given an array of strings words and a string s, determine if s is an acronym of words.
5+
6+
The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"].
7+
8+
Return true if s is an acronym of words, and false otherwise.
9+
10+
Example 1:
11+
Input: words = ["alice","bob","charlie"], s = "abc"
12+
Output: true
13+
Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
14+
15+
Example 2:
16+
Input: words = ["an","apple"], s = "a"
17+
Output: false
18+
Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively.
19+
The acronym formed by concatenating these characters is "aa".
20+
Hence, s = "a" is not the acronym.
21+
22+
Example 3:
23+
Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"
24+
Output: true
25+
Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy".
26+
Hence, s = "ngguoy" is the acronym.
27+
28+
Constraints:
29+
30+
1 <= words.length <= 100
31+
1 <= words[i].length <= 10
32+
1 <= s.length <= 100
33+
words[i] and s consist of lowercase English letters.
34+
*/
35+
package com.raj;
36+
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.List;
40+
41+
public class CheckIfStringIsAnAcronymOfWords {
42+
public static void main(String[] args) {
43+
// Initialization.
44+
List<String> words = new ArrayList<>(Arrays.asList("alice", "bob", "charlie"));
45+
String s = "abc";
46+
boolean isValid = false;
47+
48+
// Logic.
49+
if (words.size() == s.length()) {
50+
for (int i = 0; i < words.size(); i++) {
51+
if (words.get(i).charAt(0) != s.charAt(i)) {
52+
break;
53+
}
54+
}
55+
isValid = true;
56+
}
57+
58+
// Display the result.
59+
if (isValid) {
60+
System.out.println("Yes, the given string is the acronym string.");
61+
} else {
62+
System.out.println("No, the given string is not the acronym string.");
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)