Posts

Showing posts with the label String

Check If Two String Arrays are Equivalent

Given two string arrays  word1  and  word2 , return   true  if the two arrays  represent  the same string, and  false  otherwise. A string is  represented  by an array if the array elements concatenated  in order  forms the string. Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true. Example 2: Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false Example 3: Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true   Constraints: 1 <= word1.length, word2.length <= 10 3 1 <= word1[i].length, word2[i].length <= 10 3 1 <= sum(word1[i].leng

Determine if String Halves Are Alike

You are given a string   s   of even length. Split this string into two halves of equal lengths, and let   a   be the first half and   b   be the second half. Two strings are  alike  if they have the same number of vowels ( 'a' ,  'e' ,  'i' ,  'o' ,  'u' ,  'A' ,  'E' ,  'I' ,  'O' ,  'U' ). Notice that  s  contains uppercase and lowercase letters. Return  true  if  a  and  b  are  alike . Otherwise, return  false . Example 1: Input: s = "book" Output: true Explanation:  a = "b o " and b = " o k". a has 1 vowel and b has 1 vowel. Therefore, they are alike. Example 2: Input: s = "textbook" Output: false Explanation:  a = "t e xt" and b = "b oo k". a has 1 vowel whereas b has 2. Therefore, they are not alike. Notice that the vowel o is counted twice. Example 3: Input: s = "MerryChristmas" Output: false

Find all Anagrams in a String

Given a string s and a non-empty string p , find all the start indices of p 's anagrams in s . Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Example 1: Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc". Example 2: Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is an anagram of "ab". METHOD 1:   class Solution { public List<Intege

Is Subsequence?

Given a string s and a string t , check if s is subsequence of t . A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Example 1: Input: s = "abc", t = "ahbgdc" Output: true Example 2: Input: s = "axc", t = "ahbgdc" Output: false Constraints: 0 <= s.length <= 100 0 <= t.length <= 10^4 Both strings consists only of lowercase characters.   class Solution {     public boolean isSubsequence(String s, String t) {               if(s.length()==0)         return true;       in

Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Note: You may assume that both strings contain only lowercase letters. canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true   class Solution { public boolean canConstruct(String ransomNote, String magazine) { StringBuilder sb = new StringBuilder(); sb.append(magazine); int length = ransomNote.length(), count = 0; for(int i=0;i<length;i++){ int index = sb.indexOf(ransomNote.charAt(i)+""); if(index != -1){ sb.delete(index, index+1); count++;

Jewels and Stones

Image
You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A" . Example 1: Input: J = "aA", S = "aAAbbbb" Output: 3   Example 2: Input: J = "z", S = "ZZ" Output: 0   Note: S and J will consist of letters and have length at most 50. The characters in J are distinct. METHOD 1: class Solution {     public int numJewelsInStones(String J, String S) {        int res=0;        for(char c : S.toCharArray()){            if(J.indexOf(c) != -1){                res++;            }        }        return res;     } } Try it on Leetcode Here, in the problem they

Perform String Shifts - Leetcode

You are given a string s  containing lowercase English letters, and a matrix  shift , where  shift[i] = [direction, amount] : direction  can be 0  (for left shift) or 1  (for right shift).  amount  is the amount by which string  s  is to be shifted. A left shift by 1 means remove the first character of s and append it to the end. Similarly, a right shift by 1 means remove the last character of s and add it to the beginning. Return the final string after all operations. Example 1: Input: s = "abc", shift = [[0,1],[1,2]] Output: "cab" Explanation:   [0,1] means shift to left by 1. "abc" -> "bca" [1,2] means shift to right by 2. "bca" -> "cab" Example 2: Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]] Output: "efgabcd" Explanation:   [1,1] means shift to right by 1. "abcdefg" -> "gabcdef" [1,1] means shift to right by 1. "gabcdef" -> &qu

To LowerCase - Leetcode

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello"     Example 2: Input: "here" Output: "here"     Example 3: Input: "LOVELY" Output: "lovely"       class Solution { public String toLowerCase(String str) { return str.toLowerCase(); } }   Click this link to try it on Leetcode     Here string can be changed to lowercase in many ways. Option 1: Each character can be checked and changed to lowercase. It is the longest way when we are doing with Java. Because in Java there are more simple steps. Option 2: String has in-build method of toLowerCase(). For more Leetcode Problems