In this post, we will solve String Construction HackerRank Solution. This problem (String Construction) is a part of HackerRank Problem Solving series.
Solution – String Construction – HackerRank Solution
C++
#include <cmath> #include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { long t; cin>>t; while(t--) {long r=0; string s; cin>>s; vector<char> m; for(long i=0;i<s.length();++i) { if(find(m.begin(),m.end(),s[i])==m.end()) m.push_back(s[i]); } cout<<m.size()<<endl; } return 0; }
Python
import sys def stringConstruction(s): return len(set(s)) if __name__ == "__main__": q = int(input().strip()) for a0 in range(q): s = input().strip() result = stringConstruction(s) print(result)
Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int a0 = 0; a0 < n; a0++){ String s = in.next(); Set<Character> uniqueChars = new HashSet<>(); for(char c : s.toCharArray()) { uniqueChars.add(c); } System.out.println(uniqueChars.size()); } } }
Note: This problem (String Construction) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.